mirror of
https://github.com/kristoferssolo/hexlab.git
synced 2025-10-21 19:40:34 +00:00
Compare commits
No commits in common. "main" and "v0.4.0" have entirely different histories.
90
.github/workflows/ci.yml
vendored
90
.github/workflows/ci.yml
vendored
@ -9,30 +9,76 @@ env:
|
|||||||
RUSTFLAGS: --deny warnings
|
RUSTFLAGS: --deny warnings
|
||||||
RUSTDOCFLAGS: --deny warnings
|
RUSTDOCFLAGS: --deny warnings
|
||||||
jobs:
|
jobs:
|
||||||
build-and-test:
|
# Run tests.
|
||||||
|
test:
|
||||||
|
name: Tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
timeout-minutes: 30
|
||||||
SCCACHE_GHA_ENABLED: "true"
|
|
||||||
RUSTC_WRAPPER: "sccache"
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v4
|
||||||
- name: Install Rust
|
- name: Install Rust toolchain
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
- name: Install dependencies
|
||||||
|
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
|
||||||
|
- name: Populate target directory from cache
|
||||||
|
uses: Leafwing-Studios/cargo-cache@v2
|
||||||
|
with:
|
||||||
|
sweep-cache: true
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
cargo test --locked --workspace --all-features --all-targets
|
||||||
|
# Workaround for https://github.com/rust-lang/cargo/issues/6669
|
||||||
|
cargo test --locked --workspace --all-features --doc
|
||||||
|
# Run clippy lints.
|
||||||
|
clippy:
|
||||||
|
name: Clippy
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install Rust toolchain
|
||||||
uses: dtolnay/rust-toolchain@stable
|
uses: dtolnay/rust-toolchain@stable
|
||||||
with:
|
with:
|
||||||
toolchain: stable
|
components: clippy
|
||||||
components: clippy, rustfmt
|
- name: Install dependencies
|
||||||
- name: Run sccache-cache
|
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
|
||||||
uses: mozilla-actions/sccache-action@v0.0.9
|
- name: Populate target directory from cache
|
||||||
- name: Install cargo-nextest
|
uses: Leafwing-Studios/cargo-cache@v2
|
||||||
uses: taiki-e/install-action@cargo-nextest
|
with:
|
||||||
- name: Run Clippy
|
sweep-cache: true
|
||||||
run: cargo clippy --locked --workspace --all-targets --all-features -- -D warnings
|
- name: Run clippy lints
|
||||||
- name: Run formatting
|
run: cargo clippy --locked --workspace --all-features -- --deny warnings
|
||||||
run: cargo fmt --all --check
|
# Check formatting.
|
||||||
- name: Run Tests
|
format:
|
||||||
run: |
|
name: Format
|
||||||
cargo nextest run --all-features --all-targets
|
runs-on: ubuntu-latest
|
||||||
cargo test --locked --workspace --all-features --doc
|
timeout-minutes: 30
|
||||||
- name: Check Documentation
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install Rust toolchain
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
with:
|
||||||
|
components: rustfmt
|
||||||
|
- name: Run cargo fmt
|
||||||
|
run: cargo fmt --all -- --check
|
||||||
|
# Check documentation.
|
||||||
|
doc:
|
||||||
|
name: Docs
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install Rust toolchain
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
- name: Install dependencies
|
||||||
|
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
|
||||||
|
- name: Populate target directory from cache
|
||||||
|
uses: Leafwing-Studios/cargo-cache@v2
|
||||||
|
with:
|
||||||
|
sweep-cache: true
|
||||||
|
- name: Check documentation
|
||||||
run: cargo doc --locked --workspace --all-features --document-private-items --no-deps
|
run: cargo doc --locked --workspace --all-features --document-private-items --no-deps
|
||||||
|
|||||||
107
Cargo.lock
generated
107
Cargo.lock
generated
@ -1248,7 +1248,7 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"regex",
|
"regex",
|
||||||
"rustc-hash 1.1.0",
|
"rustc-hash",
|
||||||
"shlex",
|
"shlex",
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
@ -1631,7 +1631,7 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"rangemap",
|
"rangemap",
|
||||||
"rayon",
|
"rayon",
|
||||||
"rustc-hash 1.1.0",
|
"rustc-hash",
|
||||||
"rustybuzz",
|
"rustybuzz",
|
||||||
"self_cell",
|
"self_cell",
|
||||||
"swash",
|
"swash",
|
||||||
@ -1743,18 +1743,6 @@ version = "2.6.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2"
|
checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "deprecate-until"
|
|
||||||
version = "0.1.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7a3767f826efbbe5a5ae093920b58b43b01734202be697e1354914e862e8e704"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"semver",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "derive_more"
|
name = "derive_more"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -2026,6 +2014,21 @@ version = "0.3.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
|
checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
|
||||||
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
|
"futures-core",
|
||||||
|
"futures-executor",
|
||||||
|
"futures-io",
|
||||||
|
"futures-sink",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-channel"
|
name = "futures-channel"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@ -2033,6 +2036,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2041,6 +2045,17 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-executor"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-io"
|
name = "futures-io"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@ -2071,6 +2086,12 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-sink"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-task"
|
name = "futures-task"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@ -2089,9 +2110,13 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-io",
|
||||||
"futures-macro",
|
"futures-macro",
|
||||||
|
"futures-sink",
|
||||||
"futures-task",
|
"futures-task",
|
||||||
|
"memchr",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
"slab",
|
"slab",
|
||||||
@ -2350,7 +2375,7 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hexlab"
|
name = "hexlab"
|
||||||
version = "0.6.1"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"bevy_reflect",
|
"bevy_reflect",
|
||||||
@ -2358,7 +2383,6 @@ dependencies = [
|
|||||||
"claims",
|
"claims",
|
||||||
"glam",
|
"glam",
|
||||||
"hexx",
|
"hexx",
|
||||||
"pathfinding",
|
|
||||||
"rand",
|
"rand",
|
||||||
"rstest",
|
"rstest",
|
||||||
"serde",
|
"serde",
|
||||||
@ -2367,9 +2391,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hexx"
|
name = "hexx"
|
||||||
version = "0.19.1"
|
version = "0.19.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4b450e02a24a4a981c895be4cd2752e2401996c545971309730c4e812b984691"
|
checksum = "34b47b6f7ee46bba869534a92306b7e7f549bec38114a4ba0288b9321617db22"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy_reflect",
|
"bevy_reflect",
|
||||||
"glam",
|
"glam",
|
||||||
@ -2433,15 +2457,6 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "integer-sqrt"
|
|
||||||
version = "0.1.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "276ec31bcb4a9ee45f58bec6f9ec700ae4cf4f4f8f2fa7e06cb406bd5ffdd770"
|
|
||||||
dependencies = [
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "io-kit-sys"
|
name = "io-kit-sys"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
@ -2709,7 +2724,7 @@ dependencies = [
|
|||||||
"indexmap",
|
"indexmap",
|
||||||
"log",
|
"log",
|
||||||
"pp-rs",
|
"pp-rs",
|
||||||
"rustc-hash 1.1.0",
|
"rustc-hash",
|
||||||
"spirv",
|
"spirv",
|
||||||
"termcolor",
|
"termcolor",
|
||||||
"thiserror 1.0.69",
|
"thiserror 1.0.69",
|
||||||
@ -2730,7 +2745,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"regex",
|
"regex",
|
||||||
"regex-syntax 0.8.5",
|
"regex-syntax 0.8.5",
|
||||||
"rustc-hash 1.1.0",
|
"rustc-hash",
|
||||||
"thiserror 1.0.69",
|
"thiserror 1.0.69",
|
||||||
"tracing",
|
"tracing",
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
@ -3188,20 +3203,6 @@ version = "1.0.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pathfinding"
|
|
||||||
version = "4.13.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "301ad6aa19104eeb9af172b3d6a4ab8a5ea26234890baf2fcb1cbbc3f05f674b"
|
|
||||||
dependencies = [
|
|
||||||
"deprecate-until",
|
|
||||||
"indexmap",
|
|
||||||
"integer-sqrt",
|
|
||||||
"num-traits",
|
|
||||||
"rustc-hash 2.1.0",
|
|
||||||
"thiserror 2.0.3",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
version = "2.3.1"
|
version = "2.3.1"
|
||||||
@ -3569,21 +3570,21 @@ checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rstest"
|
name = "rstest"
|
||||||
version = "0.24.0"
|
version = "0.23.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "03e905296805ab93e13c1ec3a03f4b6c4f35e9498a3d5fa96dc626d22c03cd89"
|
checksum = "0a2c585be59b6b5dd66a9d2084aa1d8bd52fbdb806eafdeffb52791147862035"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"futures",
|
||||||
"futures-timer",
|
"futures-timer",
|
||||||
"futures-util",
|
|
||||||
"rstest_macros",
|
"rstest_macros",
|
||||||
"rustc_version",
|
"rustc_version",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rstest_macros"
|
name = "rstest_macros"
|
||||||
version = "0.24.0"
|
version = "0.23.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ef0053bbffce09062bee4bcc499b0fbe7a57b879f1efe088d6d8d4c7adcdef9b"
|
checksum = "825ea780781b15345a146be27eaefb05085e337e869bff01b4306a4fd4a9ad5a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"glob",
|
"glob",
|
||||||
@ -3603,12 +3604,6 @@ version = "1.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rustc-hash"
|
|
||||||
version = "2.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc_version"
|
name = "rustc_version"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
@ -4325,7 +4320,7 @@ dependencies = [
|
|||||||
"parking_lot",
|
"parking_lot",
|
||||||
"profiling",
|
"profiling",
|
||||||
"raw-window-handle",
|
"raw-window-handle",
|
||||||
"rustc-hash 1.1.0",
|
"rustc-hash",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"thiserror 1.0.69",
|
"thiserror 1.0.69",
|
||||||
"wgpu-hal",
|
"wgpu-hal",
|
||||||
@ -4367,7 +4362,7 @@ dependencies = [
|
|||||||
"range-alloc",
|
"range-alloc",
|
||||||
"raw-window-handle",
|
"raw-window-handle",
|
||||||
"renderdoc-sys",
|
"renderdoc-sys",
|
||||||
"rustc-hash 1.1.0",
|
"rustc-hash",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"thiserror 1.0.69",
|
"thiserror 1.0.69",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "hexlab"
|
name = "hexlab"
|
||||||
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
||||||
version = "0.6.1"
|
version = "0.4.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "A hexagonal maze generation and manipulation library"
|
description = "A hexagonal maze generation and manipulation library"
|
||||||
repository = "https://github.com/kristoferssolo/hexlab"
|
repository = "https://github.com/kristoferssolo/hexlab"
|
||||||
@ -26,7 +26,6 @@ thiserror = "2.0"
|
|||||||
bevy = { version = "0.15", optional = true }
|
bevy = { version = "0.15", optional = true }
|
||||||
bevy_utils = { version = "0.15", optional = true }
|
bevy_utils = { version = "0.15", optional = true }
|
||||||
glam = { version = "0.29", optional = true }
|
glam = { version = "0.29", optional = true }
|
||||||
pathfinding = { version = "4.13", optional = true }
|
|
||||||
|
|
||||||
|
|
||||||
[dependencies.bevy_reflect]
|
[dependencies.bevy_reflect]
|
||||||
@ -37,7 +36,7 @@ features = ["glam"]
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
claims = "0.8"
|
claims = "0.8"
|
||||||
rstest = "0.24"
|
rstest = "0.23"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
@ -49,8 +48,7 @@ bevy_reflect = [
|
|||||||
"hexx/bevy_reflect",
|
"hexx/bevy_reflect",
|
||||||
"dep:glam",
|
"dep:glam",
|
||||||
]
|
]
|
||||||
pathfinding = ["dep:pathfinding"]
|
full = ["serde", "bevy"]
|
||||||
full = ["serde", "bevy", "pathfinding"]
|
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 1 # Better compile times with some optimization
|
opt-level = 1 # Better compile times with some optimization
|
||||||
|
|||||||
@ -42,7 +42,7 @@ fn main() {
|
|||||||
.with_radius(5)
|
.with_radius(5)
|
||||||
.build()
|
.build()
|
||||||
.expect("Failed to create maze");
|
.expect("Failed to create maze");
|
||||||
println!("Maze size: {}", maze.count());
|
println!("Maze size: {}", maze.len());
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,25 @@
|
|||||||
use crate::{errors::MazeBuilderError, GeneratorType, Maze};
|
use crate::{GeneratorType, HexMaze};
|
||||||
use hexx::Hex;
|
use hexx::Hex;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum MazeBuilderError {
|
||||||
|
/// Occurs when attempting to build a maze without specifying a radius.
|
||||||
|
#[error("Radius must be specified to build a maze")]
|
||||||
|
NoRadius,
|
||||||
|
|
||||||
|
/// Occurs when the specified radius is too large.
|
||||||
|
#[error("Radius {0} is too large. Maximum allowed radius is {1}")]
|
||||||
|
RadiusTooLarge(u32, u32),
|
||||||
|
|
||||||
|
/// Occurs when the specified start position is outside the maze bounds.
|
||||||
|
#[error("Start position {0:?} is outside maze bounds")]
|
||||||
|
InvalidStartPosition(Hex),
|
||||||
|
|
||||||
|
/// Occurs when maze generation fails.
|
||||||
|
#[error("Failed to generate maze: {0}")]
|
||||||
|
GenerationError(String),
|
||||||
|
}
|
||||||
|
|
||||||
/// A builder pattern for creating hexagonal mazes.
|
/// A builder pattern for creating hexagonal mazes.
|
||||||
///
|
///
|
||||||
@ -20,7 +40,7 @@ use hexx::Hex;
|
|||||||
///
|
///
|
||||||
/// // A radius of 5 creates 61 hexagonal tiles
|
/// // A radius of 5 creates 61 hexagonal tiles
|
||||||
/// assert!(!maze.is_empty());
|
/// assert!(!maze.is_empty());
|
||||||
/// assert_eq!(maze.count(), 91);
|
/// assert_eq!(maze.len(), 91);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Using a seed for reproducible results:
|
/// Using a seed for reproducible results:
|
||||||
@ -40,7 +60,7 @@ use hexx::Hex;
|
|||||||
/// .expect("Failed to create maze");
|
/// .expect("Failed to create maze");
|
||||||
///
|
///
|
||||||
/// // Same seed should produce identical mazes
|
/// // Same seed should produce identical mazes
|
||||||
/// assert_eq!(maze1.count(), maze2.count());
|
/// assert_eq!(maze1.len(), maze2.len());
|
||||||
/// assert_eq!(maze1, maze2);
|
/// assert_eq!(maze1, maze2);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@ -65,7 +85,7 @@ pub struct MazeBuilder {
|
|||||||
|
|
||||||
impl MazeBuilder {
|
impl MazeBuilder {
|
||||||
/// Creates a new [`MazeBuilder`] instance with default settings.
|
/// Creates a new [`MazeBuilder`] instance with default settings.
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
@ -81,7 +101,7 @@ impl MazeBuilder {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `radius` - The number of tiles from the center to the edge of the hexagon.
|
/// - `radius` - The number of tiles from the center to the edge of the hexagon.
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_radius(mut self, radius: u16) -> Self {
|
pub const fn with_radius(mut self, radius: u16) -> Self {
|
||||||
self.radius = Some(radius);
|
self.radius = Some(radius);
|
||||||
@ -95,7 +115,7 @@ impl MazeBuilder {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `seed` - The random seed value.
|
/// - `seed` - The random seed value.
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_seed(mut self, seed: u64) -> Self {
|
pub const fn with_seed(mut self, seed: u64) -> Self {
|
||||||
self.seed = Some(seed);
|
self.seed = Some(seed);
|
||||||
@ -109,7 +129,7 @@ impl MazeBuilder {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `generator_type` - The maze generation algorithm to use.
|
/// - `generator_type` - The maze generation algorithm to use.
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_generator(mut self, generator_type: GeneratorType) -> Self {
|
pub const fn with_generator(mut self, generator_type: GeneratorType) -> Self {
|
||||||
self.generator_type = generator_type;
|
self.generator_type = generator_type;
|
||||||
@ -120,7 +140,7 @@ impl MazeBuilder {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `pos` - The hexagonal coordinates for the starting position.
|
/// - `pos` - The hexagonal coordinates for the starting position.
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_start_position(mut self, pos: Hex) -> Self {
|
pub const fn with_start_position(mut self, pos: Hex) -> Self {
|
||||||
self.start_position = Some(pos);
|
self.start_position = Some(pos);
|
||||||
@ -152,12 +172,12 @@ impl MazeBuilder {
|
|||||||
/// let maze = result.unwrap();
|
/// let maze = result.unwrap();
|
||||||
/// assert!(!maze.is_empty());
|
/// assert!(!maze.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn build(self) -> Result<Maze, MazeBuilderError> {
|
pub fn build(self) -> Result<HexMaze, MazeBuilderError> {
|
||||||
let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?;
|
let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?;
|
||||||
let mut maze = create_hex_maze(radius);
|
let mut maze = create_hex_maze(radius);
|
||||||
|
|
||||||
if let Some(start_pos) = self.start_position {
|
if let Some(start_pos) = self.start_position {
|
||||||
if maze.get(&start_pos).is_none() {
|
if maze.get_tile(&start_pos).is_none() {
|
||||||
return Err(MazeBuilderError::InvalidStartPosition(start_pos));
|
return Err(MazeBuilderError::InvalidStartPosition(start_pos));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,9 +190,8 @@ impl MazeBuilder {
|
|||||||
Ok(maze)
|
Ok(maze)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn create_hex_maze(radius: u16) -> HexMaze {
|
||||||
pub fn create_hex_maze(radius: u16) -> Maze {
|
let mut maze = HexMaze::new();
|
||||||
let mut maze = Maze::new();
|
|
||||||
let radius = i32::from(radius);
|
let radius = i32::from(radius);
|
||||||
|
|
||||||
for q in -radius..=radius {
|
for q in -radius..=radius {
|
||||||
@ -180,7 +199,7 @@ pub fn create_hex_maze(radius: u16) -> Maze {
|
|||||||
let r2 = radius.min(-q + radius);
|
let r2 = radius.min(-q + radius);
|
||||||
for r in r1..=r2 {
|
for r in r1..=r2 {
|
||||||
let pos = Hex::new(q, r);
|
let pos = Hex::new(q, r);
|
||||||
maze.insert(pos);
|
maze.add_tile(pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +209,7 @@ pub fn create_hex_maze(radius: u16) -> Maze {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use claims::{assert_gt, assert_some};
|
use claims::assert_gt;
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -211,18 +230,18 @@ mod test {
|
|||||||
#[case(100, 30301)]
|
#[case(100, 30301)]
|
||||||
fn create_hex_maze_various_radii(#[case] radius: u16, #[case] expected_size: usize) {
|
fn create_hex_maze_various_radii(#[case] radius: u16, #[case] expected_size: usize) {
|
||||||
let maze = create_hex_maze(radius);
|
let maze = create_hex_maze(radius);
|
||||||
assert_eq!(maze.count(), expected_size);
|
assert_eq!(maze.len(), expected_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_hex_maze_large_radius() {
|
fn create_hex_maze_large_radius() {
|
||||||
let large_radius = 1000;
|
let large_radius = 1000;
|
||||||
let maze = create_hex_maze(large_radius);
|
let maze = create_hex_maze(large_radius);
|
||||||
assert_gt!(maze.count(), 0);
|
assert_gt!(maze.len(), 0);
|
||||||
|
|
||||||
// Calculate expected size for this radius
|
// Calculate expected size for this radius
|
||||||
let expected_size = 3 * (large_radius as usize).pow(2) + 3 * large_radius as usize + 1;
|
let expected_size = 3 * (large_radius as usize).pow(2) + 3 * large_radius as usize + 1;
|
||||||
assert_eq!(maze.count(), expected_size);
|
assert_eq!(maze.len(), expected_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -249,8 +268,8 @@ mod test {
|
|||||||
Hex::new(0, -2),
|
Hex::new(0, -2),
|
||||||
Hex::new(1, -2),
|
Hex::new(1, -2),
|
||||||
];
|
];
|
||||||
for pos in &expected_positions {
|
for pos in expected_positions.iter() {
|
||||||
assert_some!(maze.get(pos), "Expected tile at {pos:?}");
|
assert!(maze.get_tile(pos).is_some(), "Expected tile at {:?}", pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,41 +0,0 @@
|
|||||||
use hexx::{EdgeDirection, Hex};
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
use crate::Tile;
|
|
||||||
|
|
||||||
#[derive(Debug, Error, PartialEq, Eq)]
|
|
||||||
pub enum MazeBuilderError {
|
|
||||||
/// Occurs when attempting to build a maze without specifying a radius.
|
|
||||||
#[error("Radius must be specified to build a maze")]
|
|
||||||
NoRadius,
|
|
||||||
|
|
||||||
/// Occurs when the specified start position is outside the maze bounds.
|
|
||||||
#[error("Start position {0:?} is outside maze bounds")]
|
|
||||||
InvalidStartPosition(Hex),
|
|
||||||
|
|
||||||
/// Occurs when maze generation fails.
|
|
||||||
#[error("Failed to generate maze: {0}")]
|
|
||||||
GenerationError(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Error, PartialEq, Eq)]
|
|
||||||
pub enum MazeError {
|
|
||||||
/// Error when attempting to access or modify a tile at a non-existent coordinate.
|
|
||||||
#[error("Invalid coordinate: {0:?}")]
|
|
||||||
InvalidCoordinate(Hex),
|
|
||||||
|
|
||||||
/// Error when a tile's internal position doesn't match its insertion coordinate.
|
|
||||||
#[error("Tile position ({tile_pos:?}) does not match insertion coordinates ({insert_pos:?})")]
|
|
||||||
PositionMismatch { tile_pos: Hex, insert_pos: Hex },
|
|
||||||
|
|
||||||
/// Error when attempting to insert a tile at an already occupied position.
|
|
||||||
#[error("A tile {old_tile:?} already exists at position {pos:?}")]
|
|
||||||
TileAlreadyExists { pos: Hex, old_tile: Tile },
|
|
||||||
|
|
||||||
/// Error when a wall operation fails at the specified coordinate and direction.
|
|
||||||
#[error("Cannot add wall at {coord:?} in direction {direction:?}")]
|
|
||||||
WallOperationFailed {
|
|
||||||
coord: Hex,
|
|
||||||
direction: EdgeDirection,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
@ -1,9 +1,9 @@
|
|||||||
use crate::Maze;
|
use crate::HexMaze;
|
||||||
use hexx::{EdgeDirection, Hex};
|
use hexx::{EdgeDirection, Hex};
|
||||||
use rand::{rngs::StdRng, seq::SliceRandom, thread_rng, Rng, RngCore, SeedableRng};
|
use rand::{rngs::StdRng, seq::SliceRandom, thread_rng, Rng, RngCore, SeedableRng};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
pub(super) fn generate_backtracking(maze: &mut Maze, start_pos: Option<Hex>, seed: Option<u64>) {
|
pub(super) fn generate_backtracking(maze: &mut HexMaze, start_pos: Option<Hex>, seed: Option<u64>) {
|
||||||
if maze.is_empty() {
|
if maze.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ pub(super) fn generate_backtracking(maze: &mut Maze, start_pos: Option<Hex>, see
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn recursive_backtrack<R: Rng>(
|
fn recursive_backtrack<R: Rng>(
|
||||||
maze: &mut Maze,
|
maze: &mut HexMaze,
|
||||||
current: Hex,
|
current: Hex,
|
||||||
visited: &mut HashSet<Hex>,
|
visited: &mut HashSet<Hex>,
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
@ -32,9 +32,9 @@ fn recursive_backtrack<R: Rng>(
|
|||||||
|
|
||||||
for direction in directions {
|
for direction in directions {
|
||||||
let neighbor = current + direction;
|
let neighbor = current + direction;
|
||||||
if maze.get(&neighbor).is_some() && !visited.contains(&neighbor) {
|
if maze.get_tile(&neighbor).is_some() && !visited.contains(&neighbor) {
|
||||||
let _ = maze.remove_tile_wall(¤t, direction);
|
maze.remove_tile_wall(¤t, direction);
|
||||||
let _ = maze.remove_tile_wall(&neighbor, direction.const_neg());
|
maze.remove_tile_wall(&neighbor, direction.const_neg());
|
||||||
recursive_backtrack(maze, neighbor, visited, rng);
|
recursive_backtrack(maze, neighbor, visited, rng);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -44,7 +44,6 @@ fn recursive_backtrack<R: Rng>(
|
|||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::builder::create_hex_maze;
|
use crate::builder::create_hex_maze;
|
||||||
use claims::assert_some;
|
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
@ -73,7 +72,7 @@ mod test {
|
|||||||
recursive_backtrack(&mut maze, start, &mut visited, &mut rng);
|
recursive_backtrack(&mut maze, start, &mut visited, &mut rng);
|
||||||
|
|
||||||
for &pos in maze.keys() {
|
for &pos in maze.keys() {
|
||||||
let walls = assert_some!(maze.get_walls(&pos));
|
let walls = maze.get_walls(&pos).unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
walls.count() < 6,
|
walls.count() < 6,
|
||||||
"At least one wall should be removed for each tile"
|
"At least one wall should be removed for each tile"
|
||||||
@ -101,16 +100,12 @@ mod test {
|
|||||||
for dir in EdgeDirection::ALL_DIRECTIONS {
|
for dir in EdgeDirection::ALL_DIRECTIONS {
|
||||||
let neighbor = current + dir;
|
let neighbor = current + dir;
|
||||||
if let Some(walls) = maze.get_walls(¤t) {
|
if let Some(walls) = maze.get_walls(¤t) {
|
||||||
if !walls.contains(dir) && maze.get(&neighbor).is_some() {
|
if !walls.contains(dir) && maze.get_tile(&neighbor).is_some() {
|
||||||
to_visit.push(neighbor);
|
to_visit.push(neighbor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(connected.len(), maze.len(), "All tiles should be connected");
|
||||||
connected.len(),
|
|
||||||
maze.count(),
|
|
||||||
"All tiles should be connected"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
mod backtrack;
|
mod backtrack;
|
||||||
use crate::Maze;
|
use crate::HexMaze;
|
||||||
use backtrack::generate_backtracking;
|
use backtrack::generate_backtracking;
|
||||||
#[cfg(feature = "bevy")]
|
#[cfg(feature = "bevy")]
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@ -15,9 +15,8 @@ pub enum GeneratorType {
|
|||||||
#[default]
|
#[default]
|
||||||
RecursiveBacktracking,
|
RecursiveBacktracking,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GeneratorType {
|
impl GeneratorType {
|
||||||
pub fn generate(&self, maze: &mut Maze, start_pos: Option<Hex>, seed: Option<u64>) {
|
pub fn generate(&self, maze: &mut HexMaze, start_pos: Option<Hex>, seed: Option<u64>) {
|
||||||
match self {
|
match self {
|
||||||
Self::RecursiveBacktracking => generate_backtracking(maze, start_pos, seed),
|
Self::RecursiveBacktracking => generate_backtracking(maze, start_pos, seed),
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/lib.rs
21
src/lib.rs
@ -7,6 +7,7 @@
|
|||||||
//! - Efficient bit-flag representation of walls
|
//! - Efficient bit-flag representation of walls
|
||||||
//! - Multiple maze generation algorithms
|
//! - Multiple maze generation algorithms
|
||||||
//! - Maze builder pattern for easy maze creation
|
//! - Maze builder pattern for easy maze creation
|
||||||
|
|
||||||
//!
|
//!
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
@ -20,7 +21,7 @@
|
|||||||
//! .build()
|
//! .build()
|
||||||
//! .expect("Failed to create maze");
|
//! .expect("Failed to create maze");
|
||||||
//!
|
//!
|
||||||
//! assert_eq!(maze.count(), 37); // A radius of 3 should create 37 tiles
|
//! assert_eq!(maze.len(), 37); // A radius of 3 should create 37 tiles
|
||||||
//!```
|
//!```
|
||||||
//!
|
//!
|
||||||
//! Customizing maze generation:
|
//! Customizing maze generation:
|
||||||
@ -35,7 +36,7 @@
|
|||||||
//! .build()
|
//! .build()
|
||||||
//! .expect("Failed to create maze");
|
//! .expect("Failed to create maze");
|
||||||
//!
|
//!
|
||||||
//! assert!(maze.get(&Hex::new(1, -1)).is_some());
|
//! assert!(maze.get_tile(&Hex::new(1, -1)).is_some());
|
||||||
//!```
|
//!```
|
||||||
//!
|
//!
|
||||||
//! Manipulating walls:
|
//! Manipulating walls:
|
||||||
@ -44,30 +45,24 @@
|
|||||||
//! use hexlab::prelude::*;
|
//! use hexlab::prelude::*;
|
||||||
//!
|
//!
|
||||||
//! let mut walls = Walls::empty();
|
//! let mut walls = Walls::empty();
|
||||||
//! assert!(!walls.insert(EdgeDirection::FLAT_NORTH));
|
//! walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
//! assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
//! assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
||||||
//! assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
//! assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
||||||
//!```
|
//!```
|
||||||
mod builder;
|
mod builder;
|
||||||
pub mod errors;
|
|
||||||
mod generator;
|
mod generator;
|
||||||
mod maze;
|
mod maze;
|
||||||
#[cfg(feature = "pathfinding")]
|
|
||||||
mod pathfinding;
|
|
||||||
mod tile;
|
mod tile;
|
||||||
pub mod traits;
|
|
||||||
mod walls;
|
mod walls;
|
||||||
|
|
||||||
pub use builder::MazeBuilder;
|
pub use builder::{MazeBuilder, MazeBuilderError};
|
||||||
pub use errors::*;
|
|
||||||
pub use generator::GeneratorType;
|
pub use generator::GeneratorType;
|
||||||
pub use maze::Maze;
|
pub use maze::HexMaze;
|
||||||
pub use tile::Tile;
|
pub use tile::HexTile;
|
||||||
pub use traits::*;
|
|
||||||
pub use walls::Walls;
|
pub use walls::Walls;
|
||||||
|
|
||||||
/// Prelude module containing commonly used types
|
/// Prelude module containing commonly used types
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use super::{errors::*, traits::*, GeneratorType, Maze, MazeBuilder, Tile, Walls};
|
pub use super::{GeneratorType, HexMaze, HexTile, MazeBuilder, MazeBuilderError, Walls};
|
||||||
pub use hexx::{EdgeDirection, Hex, HexLayout};
|
pub use hexx::{EdgeDirection, Hex, HexLayout};
|
||||||
}
|
}
|
||||||
|
|||||||
281
src/maze.rs
281
src/maze.rs
@ -1,8 +1,4 @@
|
|||||||
use super::{Tile, Walls};
|
use super::{HexTile, Walls};
|
||||||
use crate::{
|
|
||||||
errors::MazeError,
|
|
||||||
traits::{TilePosition, WallStorage},
|
|
||||||
};
|
|
||||||
#[cfg(feature = "bevy")]
|
#[cfg(feature = "bevy")]
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
#[cfg(feature = "bevy_reflect")]
|
#[cfg(feature = "bevy_reflect")]
|
||||||
@ -16,14 +12,15 @@ use std::ops::{Deref, DerefMut};
|
|||||||
///
|
///
|
||||||
/// This struct stores the layout of a hexagonal maze, including the positions
|
/// This struct stores the layout of a hexagonal maze, including the positions
|
||||||
/// of tiles and their associated walls.
|
/// of tiles and their associated walls.
|
||||||
|
#[allow(clippy::module_name_repetitions)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
|
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
|
||||||
#[cfg_attr(feature = "bevy", derive(Component))]
|
#[cfg_attr(feature = "bevy", derive(Component))]
|
||||||
#[cfg_attr(feature = "bevy", reflect(Component))]
|
#[cfg_attr(feature = "bevy", reflect(Component))]
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||||
pub struct Maze(HashMap<Hex, Tile>);
|
pub struct HexMaze(HashMap<Hex, HexTile>);
|
||||||
|
|
||||||
impl Maze {
|
impl HexMaze {
|
||||||
/// Creates a new empty maze
|
/// Creates a new empty maze
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -31,23 +28,18 @@ impl Maze {
|
|||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let maze = Maze::new();
|
/// let maze = HexMaze::new();
|
||||||
///
|
///
|
||||||
/// assert!(maze.is_empty());
|
/// assert!(maze.is_empty());
|
||||||
/// assert_eq!(maze.count(), 0);
|
/// assert_eq!(maze.len(), 0);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Inserts a new tile at the specified coordinates.
|
/// Adds a new tile at the specified coordinates
|
||||||
///
|
|
||||||
/// If the map did not have this key present, [`None`] is returned.
|
|
||||||
///
|
|
||||||
/// If the map did have this key present, the value is updated, and the old
|
|
||||||
/// value is returned.
|
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
@ -58,70 +50,43 @@ impl Maze {
|
|||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut maze = Maze::new();
|
/// let mut maze = HexMaze::new();
|
||||||
/// let coord = Hex::ZERO;
|
/// let coord = Hex::ZERO;
|
||||||
|
/// maze.add_tile(coord);
|
||||||
///
|
///
|
||||||
/// assert_eq!(maze.insert(coord), None);
|
/// assert_eq!(maze.len(), 1);
|
||||||
/// assert_eq!(maze.insert(coord), Some(Tile::new(coord)));
|
/// assert!(!maze.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn insert(&mut self, coords: Hex) -> Option<Tile> {
|
pub fn add_tile(&mut self, coords: Hex) {
|
||||||
let tile = Tile::new(coords);
|
let tile = HexTile::new(coords);
|
||||||
self.0.insert(coords, tile)
|
self.0.insert(coords, tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a new tile at the specified coordinates. It is recommended to use [`insert`].
|
/// Adds a wall in the specified direction at the given coordinates.
|
||||||
///
|
|
||||||
/// [`insert`]: Maze::insert
|
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `coords` - The hexagonal coordinates where the tile should be added.
|
/// - `coord` - The hexagonal coordinates of the tile.
|
||||||
/// - `tile` - The tile to insert to.
|
/// - `direction` - The direction in which to add the wall.
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Returns [`MazeError::PositionMismatch`] if the tile's position doesn't match the insertion coordinates.
|
|
||||||
/// Returns [`MazeError::TileAlreadyExists`] if a tile already exists at the specified coordinates.
|
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut maze = Maze::new();
|
/// let mut maze = HexMaze::new();
|
||||||
|
/// let coord = Hex::ZERO;
|
||||||
|
/// maze.add_tile(coord);
|
||||||
///
|
///
|
||||||
/// assert_eq!(
|
/// maze.add_wall(coord, EdgeDirection::FLAT_NORTH);
|
||||||
/// maze.insert_with_tile(Hex::new(2, 2), Tile::new(Hex::ZERO)),
|
/// let walls = maze.get_walls(&coord);
|
||||||
/// Err(MazeError::PositionMismatch {
|
/// assert!(walls.is_some());
|
||||||
/// tile_pos: Hex::ZERO,
|
/// assert!(walls.unwrap().contains(EdgeDirection::FLAT_NORTH));
|
||||||
/// insert_pos: Hex::new(2, 2)
|
|
||||||
/// })
|
|
||||||
/// );
|
|
||||||
/// let tile = Tile::new(Hex::ZERO);
|
|
||||||
/// assert_eq!(maze.insert_with_tile(Hex::ZERO, tile.clone()), Ok(tile.clone()));
|
|
||||||
/// assert_eq!(
|
|
||||||
/// maze.insert_with_tile(Hex::ZERO, tile.clone()),
|
|
||||||
/// Err(MazeError::TileAlreadyExists {
|
|
||||||
/// pos: Hex::ZERO,
|
|
||||||
/// old_tile: tile
|
|
||||||
/// })
|
|
||||||
/// );
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn insert_with_tile(&mut self, coords: Hex, tile: Tile) -> Result<Tile, MazeError> {
|
pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) {
|
||||||
if tile.pos != coords {
|
if let Some(tile) = self.0.get_mut(&coord) {
|
||||||
return Err(MazeError::PositionMismatch {
|
tile.walls.add(direction);
|
||||||
tile_pos: tile.pos,
|
|
||||||
insert_pos: coords,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
self.0
|
|
||||||
.insert(coords, tile.clone())
|
|
||||||
.map_or(Ok(tile), |old_tile| {
|
|
||||||
Err(MazeError::TileAlreadyExists {
|
|
||||||
pos: coords,
|
|
||||||
old_tile,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reference to the tile at the specified coordinates.
|
/// Returns a reference to the tile at the specified coordinates.
|
||||||
@ -135,26 +100,20 @@ impl Maze {
|
|||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut maze = Maze::new();
|
/// let mut maze = HexMaze::new();
|
||||||
/// let coord = Hex::ZERO;
|
/// let coord = Hex::ZERO;
|
||||||
/// maze.insert(coord);
|
/// maze.add_tile(coord);
|
||||||
///
|
///
|
||||||
/// assert!(maze.get(&coord).is_some());
|
/// assert!(maze.get_tile(&coord).is_some());
|
||||||
/// assert!(maze.get(&Hex::new(1, 1)).is_none());
|
/// assert!(maze.get_tile(&Hex::new(1, 1)).is_none());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get(&self, coord: &Hex) -> Option<&Tile> {
|
pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> {
|
||||||
self.0.get(coord)
|
self.0.get(coord)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
/// Returns an optional reference to the walls at the specified coordinates.
|
||||||
#[must_use]
|
|
||||||
pub fn get_mut(&mut self, coord: &Hex) -> Option<&mut Tile> {
|
|
||||||
self.0.get_mut(coord)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an optional mutable reference to the walls at the specified coordinates.
|
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
@ -165,43 +124,16 @@ impl Maze {
|
|||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut maze = Maze::new();
|
/// let mut maze = HexMaze::new();
|
||||||
/// let coord = Hex::new(0, 0);
|
/// let coord = Hex::new(0, 0);
|
||||||
/// maze.insert(coord);
|
/// maze.add_tile(coord);
|
||||||
///
|
///
|
||||||
/// maze.add_tile_wall(&coord, EdgeDirection::FLAT_NORTH);
|
/// maze.add_wall(coord, EdgeDirection::FLAT_NORTH);
|
||||||
/// let walls = maze.get_walls(&coord).unwrap();
|
/// let walls = maze.get_walls(&coord).unwrap();
|
||||||
/// assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
/// assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> {
|
pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> {
|
||||||
self.0.get(coord).map(Tile::walls)
|
self.0.get(coord).map(HexTile::walls)
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an optional mutable reference to the walls at the specified coordinates.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// - `coord` - The hexagonal coordinates of the tile whose walls to retrieve.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use hexlab::prelude::*;
|
|
||||||
///
|
|
||||||
/// let mut maze = Maze::new();
|
|
||||||
/// let coord = Hex::new(0, 0);
|
|
||||||
/// maze.insert(coord);
|
|
||||||
///
|
|
||||||
/// maze.add_tile_wall(&coord, EdgeDirection::FLAT_NORTH);
|
|
||||||
/// let mut walls = maze.get_walls_mut(&coord).unwrap();
|
|
||||||
/// assert!(walls.remove(EdgeDirection::FLAT_NORTH));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn get_walls_mut(&mut self, coord: &Hex) -> Option<&mut Walls> {
|
|
||||||
self.0.get_mut(coord).map(Tile::walls_mut)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of tiles in the maze.
|
/// Returns the number of tiles in the maze.
|
||||||
@ -211,18 +143,18 @@ impl Maze {
|
|||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut maze = Maze::new();
|
/// let mut maze = HexMaze::new();
|
||||||
/// assert_eq!(maze.count(), 0);
|
/// assert_eq!(maze.len(), 0);
|
||||||
///
|
///
|
||||||
/// maze.insert(Hex::new(0, 0));
|
/// maze.add_tile(Hex::new(0, 0));
|
||||||
/// assert_eq!(maze.count(), 1);
|
/// assert_eq!(maze.len(), 1);
|
||||||
///
|
///
|
||||||
/// maze.insert(Hex::new(1, -1));
|
/// maze.add_tile(Hex::new(1, -1));
|
||||||
/// assert_eq!(maze.count(), 2);
|
/// assert_eq!(maze.len(), 2);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn count(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.0.len()
|
self.0.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,147 +165,54 @@ impl Maze {
|
|||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut maze = Maze::new();
|
/// let mut maze = HexMaze::new();
|
||||||
/// assert!(maze.is_empty());
|
/// assert!(maze.is_empty());
|
||||||
///
|
///
|
||||||
/// maze.insert(Hex::ZERO);
|
/// maze.add_tile(Hex::ZERO);
|
||||||
/// assert!(!maze.is_empty());
|
/// assert!(!maze.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.0.is_empty()
|
self.0.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a wall from a tile in the specified direction.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// - `coord` - The hexagonal coordinates of the tile.
|
|
||||||
/// - `direction` - The direction of the wall to remove.
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Returns `MazeError::InvalidCoordinate` if the specified coordinate does not exist in the maze.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use hexlab::prelude::*;
|
|
||||||
///
|
|
||||||
/// // Create a maze with a single tile at the origin
|
|
||||||
/// let mut tile = Tile::new(Hex::ZERO);
|
|
||||||
/// tile.walls_mut().toggle(Walls::all_directions());
|
|
||||||
/// let mut maze = Maze::from([tile]);
|
|
||||||
///
|
|
||||||
/// // Initially, the tile should have no walls
|
|
||||||
/// assert!(maze.get_walls(&Hex::ZERO).unwrap().is_empty());
|
|
||||||
///
|
|
||||||
/// // Add a wall to the north
|
|
||||||
/// assert!(maze.add_tile_wall(&Hex::ZERO, EdgeDirection::FLAT_NORTH).is_ok());
|
|
||||||
///
|
|
||||||
/// // Check that the wall was added
|
|
||||||
/// let walls = maze.get_walls(&Hex::ZERO).unwrap();
|
|
||||||
/// assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
|
||||||
/// assert_eq!(walls.count(), 1);
|
|
||||||
///
|
|
||||||
/// // Adding the same wall again should return true (no change)
|
|
||||||
/// assert_eq!(maze.add_tile_wall(&Hex::ZERO, EdgeDirection::FLAT_NORTH), Ok(true));
|
|
||||||
///
|
|
||||||
/// // Adding a wall to a non-existent tile should return an error
|
|
||||||
/// let invalid_coord = Hex::new(1, 1);
|
|
||||||
/// assert_eq!(
|
|
||||||
/// maze.add_tile_wall(&invalid_coord, EdgeDirection::FLAT_NORTH),
|
|
||||||
/// Err(MazeError::InvalidCoordinate(invalid_coord))
|
|
||||||
/// );
|
|
||||||
/// ```
|
|
||||||
pub fn add_tile_wall(
|
|
||||||
&mut self,
|
|
||||||
coord: &Hex,
|
|
||||||
direction: EdgeDirection,
|
|
||||||
) -> Result<bool, MazeError> {
|
|
||||||
self.0
|
|
||||||
.get_mut(coord)
|
|
||||||
.map(|tile| tile.walls.insert(direction))
|
|
||||||
.ok_or(MazeError::InvalidCoordinate(*coord))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes a wall from a tile in the specified direction.
|
/// Removes a wall from a tile in the specified direction.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `coord` - The hexagonal coordinates of the tile.
|
/// - `coord` - The hexagonal coordinates of the tile.
|
||||||
/// - `direction` - The direction of the wall to remove.
|
/// - `direction` - The direction of the wall to remove.
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Returns `MazeError::InvalidCoordinate` if the specified coordinate does not exist in the maze.
|
|
||||||
///
|
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut maze = Maze::new();
|
/// let mut maze = HexMaze::new();
|
||||||
/// let coord = Hex::ZERO;
|
/// let coord = Hex::ZERO;
|
||||||
/// maze.insert(coord);
|
/// maze.add_tile(coord);
|
||||||
///
|
///
|
||||||
/// maze.add_tile_wall(&coord, EdgeDirection::FLAT_NORTH);
|
/// maze.add_wall(coord, EdgeDirection::FLAT_NORTH);
|
||||||
/// maze.remove_tile_wall(&coord, EdgeDirection::FLAT_NORTH);
|
/// maze.remove_tile_wall(&coord, EdgeDirection::FLAT_NORTH);
|
||||||
///
|
///
|
||||||
/// let walls = maze.get_walls(&coord).unwrap();
|
/// let walls = maze.get_walls(&coord).unwrap();
|
||||||
/// assert!(!walls.contains(EdgeDirection::FLAT_NORTH));
|
/// assert!(!walls.contains(EdgeDirection::FLAT_NORTH));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn remove_tile_wall(
|
pub fn remove_tile_wall(&mut self, coord: &Hex, direction: EdgeDirection) {
|
||||||
&mut self,
|
if let Some(tile) = self.0.get_mut(coord) {
|
||||||
coord: &Hex,
|
tile.walls.remove(direction);
|
||||||
direction: EdgeDirection,
|
}
|
||||||
) -> Result<bool, MazeError> {
|
|
||||||
self.0
|
|
||||||
.get_mut(coord)
|
|
||||||
.map(|tile| tile.walls.remove(direction))
|
|
||||||
.ok_or(MazeError::InvalidCoordinate(*coord))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromIterator<Hex> for Maze {
|
impl Deref for HexMaze {
|
||||||
fn from_iter<T: IntoIterator<Item = Hex>>(iter: T) -> Self {
|
type Target = HashMap<Hex, HexTile>;
|
||||||
Self(iter.into_iter().map(|hex| (hex, Tile::new(hex))).collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromIterator<Tile> for Maze {
|
|
||||||
fn from_iter<T: IntoIterator<Item = Tile>>(iter: T) -> Self {
|
|
||||||
Self(iter.into_iter().map(|tile| (tile.pos(), tile)).collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromIterator<(Hex, Tile)> for Maze {
|
|
||||||
fn from_iter<T: IntoIterator<Item = (Hex, Tile)>>(iter: T) -> Self {
|
|
||||||
Self(iter.into_iter().collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize> From<[Hex; N]> for Maze {
|
|
||||||
fn from(value: [Hex; N]) -> Self {
|
|
||||||
value.into_iter().collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize> From<[Tile; N]> for Maze {
|
|
||||||
fn from(value: [Tile; N]) -> Self {
|
|
||||||
value.into_iter().collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for Maze {
|
|
||||||
type Target = HashMap<Hex, Tile>;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DerefMut for Maze {
|
impl DerefMut for HexMaze {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
&mut self.0
|
&mut self.0
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,84 +0,0 @@
|
|||||||
//! Maze pathfinding implementation for hexagonal grids.
|
|
||||||
//!
|
|
||||||
//! This module provides functionality for finding paths through a hexagonal maze
|
|
||||||
//! using the A* pathfinding algorithm. The maze is represented as a collection of
|
|
||||||
//! hexagonal cells, where each cell may have walls on any of its six edges.
|
|
||||||
//!
|
|
||||||
//! # Examples
|
|
||||||
//!
|
|
||||||
//! ```
|
|
||||||
//! use hexlab::prelude::*;
|
|
||||||
//!
|
|
||||||
//! let maze = MazeBuilder::new()
|
|
||||||
//! .with_radius(3)
|
|
||||||
//! .with_seed(12345)
|
|
||||||
//! .build()
|
|
||||||
//! .expect("Failed to create maze");
|
|
||||||
//! assert!(maze.find_path(Hex::ZERO, Hex::new(-1, 3)).is_some());
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! # Implementation Details
|
|
||||||
//!
|
|
||||||
//! The pathfinding algorithm uses Manhattan distance as a heuristic and considers
|
|
||||||
//! walls between cells when determining valid paths. Each step between adjacent
|
|
||||||
//! cells has a cost of 1.
|
|
||||||
use hexx::{EdgeDirection, Hex};
|
|
||||||
use pathfinding::prelude::*;
|
|
||||||
|
|
||||||
use crate::Maze;
|
|
||||||
|
|
||||||
impl Maze {
|
|
||||||
#[must_use]
|
|
||||||
/// Finds the shortest path between two hexagonal positions in the maze using A* pathfinding.
|
|
||||||
///
|
|
||||||
/// This function calculates the optimal path while taking into account walls between cells.
|
|
||||||
/// The path cost between adjacent cells is always 1, and Manhattan distance is used as the
|
|
||||||
/// heuristic for pathfinding.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `from` - The starting hexagonal position
|
|
||||||
/// * `to` - The target hexagonal position
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// * `Some(Vec<Hex>)` - A vector of hexagonal positions representing the path from start to target
|
|
||||||
/// * `None` - If no valid path exists between the positions
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use hexlab::prelude::*;
|
|
||||||
///
|
|
||||||
/// let maze = MazeBuilder::new()
|
|
||||||
/// .with_radius(3)
|
|
||||||
/// .with_seed(12345)
|
|
||||||
/// .build()
|
|
||||||
/// .expect("Failed to create maze");
|
|
||||||
/// assert!(maze.find_path(Hex::ZERO, Hex::new(-1, 3)).is_some());
|
|
||||||
/// ```
|
|
||||||
pub fn find_path(&self, from: Hex, to: Hex) -> Option<Vec<Hex>> {
|
|
||||||
let successors = |pos: &Hex| {
|
|
||||||
{
|
|
||||||
EdgeDirection::ALL_DIRECTIONS.iter().filter_map(|&dir| {
|
|
||||||
let neighbor = pos.neighbor(dir);
|
|
||||||
if let Some(current_tile) = self.get(pos) {
|
|
||||||
if self.get(&neighbor).is_some() && !current_tile.walls.contains(dir) {
|
|
||||||
return Some((neighbor, 1)); // Cost of 1 for each step
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
})
|
|
||||||
}
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
};
|
|
||||||
|
|
||||||
let heuristic = |pos: &Hex| {
|
|
||||||
// Manhatan distance
|
|
||||||
let diff = *pos - to;
|
|
||||||
(diff.x.abs() + diff.y.abs() + diff.z().abs()) / 2
|
|
||||||
};
|
|
||||||
|
|
||||||
astar(&from, successors, heuristic, |pos| *pos == to).map(|(path, _)| path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
183
src/tile.rs
183
src/tile.rs
@ -1,7 +1,4 @@
|
|||||||
use super::Walls;
|
use super::Walls;
|
||||||
#[cfg(feature = "bevy_reflect")]
|
|
||||||
use crate::traits::WorldPositionable;
|
|
||||||
use crate::traits::{TilePosition, WallStorage};
|
|
||||||
#[cfg(feature = "bevy")]
|
#[cfg(feature = "bevy")]
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use hexx::Hex;
|
use hexx::Hex;
|
||||||
@ -12,90 +9,18 @@ use std::fmt::Display;
|
|||||||
/// Represents a single hexagonal tile in the maze
|
/// Represents a single hexagonal tile in the maze
|
||||||
///
|
///
|
||||||
/// Each tile has a position and a set of walls defining its boundaries.
|
/// Each tile has a position and a set of walls defining its boundaries.
|
||||||
|
#[allow(clippy::module_name_repetitions)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
|
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
|
||||||
#[cfg_attr(feature = "bevy", derive(Component))]
|
#[cfg_attr(feature = "bevy", derive(Component))]
|
||||||
#[cfg_attr(feature = "bevy", reflect(Component))]
|
#[cfg_attr(feature = "bevy", reflect(Component))]
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||||
pub struct Tile {
|
pub struct HexTile {
|
||||||
pub(crate) pos: Hex,
|
pub(crate) pos: Hex,
|
||||||
pub(crate) walls: Walls,
|
pub(crate) walls: Walls,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TilePosition for Tile {
|
impl HexTile {
|
||||||
/// Returns position of the tile
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use hexlab::prelude::*;
|
|
||||||
///
|
|
||||||
/// let tile = Tile::new(Hex::new(2, -2));
|
|
||||||
/// assert_eq!(tile.pos(), Hex::new(2, -2));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
fn pos(&self) -> Hex {
|
|
||||||
self.pos
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WallStorage for Tile {
|
|
||||||
/// Returns an immutable reference to the tile's walls
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use hexlab::prelude::*;
|
|
||||||
///
|
|
||||||
/// let tile = Tile::new(Hex::ZERO);
|
|
||||||
/// assert_eq!(*tile.walls(), Walls::default());
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
fn walls(&self) -> &Walls {
|
|
||||||
&self.walls
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a mutable reference to the tile's walls
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use hexlab::prelude::*;
|
|
||||||
///
|
|
||||||
/// let tile = Tile::new(Hex::ZERO);
|
|
||||||
/// assert_eq!(*tile.walls(), Walls::default());
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
fn walls_mut(&mut self) -> &mut Walls {
|
|
||||||
&mut self.walls
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "bevy_reflect")]
|
|
||||||
impl WorldPositionable for Tile {
|
|
||||||
/// Converts the tile's position to a 2D vector based on the given layout.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// - `layout` - The hexagonal layout used for conversion.
|
|
||||||
#[inline]
|
|
||||||
fn to_vec2(&self, layout: &HexLayout) -> glam::Vec2 {
|
|
||||||
layout.hex_to_world_pos(self.pos)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Converts the tile's position to a 3D vector based on the given layout.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// - `layout` - The hexagonal layout used for conversion.
|
|
||||||
#[inline]
|
|
||||||
fn to_vec3(&self, layout: &HexLayout) -> glam::Vec3 {
|
|
||||||
let pos = self.to_vec2(layout);
|
|
||||||
glam::Vec3::new(pos.x, 0., pos.y)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Tile {
|
|
||||||
/// Creates a new tile with the given position and default walls.
|
/// Creates a new tile with the given position and default walls.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
@ -107,7 +32,7 @@ impl Tile {
|
|||||||
/// ```
|
/// ```
|
||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let tile = Tile::new(Hex::new(1, -1));
|
/// let tile = HexTile::new(Hex::new(1, -1));
|
||||||
/// assert_eq!(tile.pos(), Hex::new(1, -1));
|
/// assert_eq!(tile.pos(), Hex::new(1, -1));
|
||||||
/// assert_eq!(*tile.walls(), Walls::default());
|
/// assert_eq!(*tile.walls(), Walls::default());
|
||||||
/// ```
|
/// ```
|
||||||
@ -118,9 +43,68 @@ impl Tile {
|
|||||||
walls: Walls::default(),
|
walls: Walls::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to the tile's walls
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use hexlab::prelude::*;
|
||||||
|
///
|
||||||
|
/// let tile = HexTile::new(Hex::ZERO);
|
||||||
|
/// assert_eq!(*tile.walls(), Walls::default());
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn walls(&self) -> &Walls {
|
||||||
|
&self.walls
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Hex> for Tile {
|
/// Returns position of the tile
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use hexlab::prelude::*;
|
||||||
|
///
|
||||||
|
/// let tile = HexTile::new(Hex::new(2, -2));
|
||||||
|
/// assert_eq!(tile.pos(), Hex::new(2, -2));
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn pos(&self) -> Hex {
|
||||||
|
self.pos
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts the tile's position to a 2D vector based on the given layout.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `layout` - The hexagonal layout used for conversion.
|
||||||
|
#[cfg(feature = "bevy_reflect")]
|
||||||
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn to_vec2(&self, layout: &HexLayout) -> glam::Vec2 {
|
||||||
|
layout.hex_to_world_pos(self.pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts the tile's position to a 3D vector based on the given layout.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `layout` - The hexagonal layout used for conversion.
|
||||||
|
#[cfg(feature = "bevy_reflect")]
|
||||||
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn to_vec3(&self, layout: &HexLayout) -> glam::Vec3 {
|
||||||
|
use glam::Vec3;
|
||||||
|
|
||||||
|
let pos = self.to_vec2(layout);
|
||||||
|
Vec3::new(pos.x, 0., pos.y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Hex> for HexTile {
|
||||||
fn from(value: Hex) -> Self {
|
fn from(value: Hex) -> Self {
|
||||||
Self {
|
Self {
|
||||||
pos: value,
|
pos: value,
|
||||||
@ -129,7 +113,7 @@ impl From<Hex> for Tile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Tile {
|
impl Display for HexTile {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "({},{})", self.pos.x, self.pos.y)
|
write!(f, "({},{})", self.pos.x, self.pos.y)
|
||||||
}
|
}
|
||||||
@ -146,6 +130,19 @@ mod test {
|
|||||||
Hex::new(rng.gen(), rng.gen())
|
Hex::new(rng.gen(), rng.gen())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tile_modification() {
|
||||||
|
let hex = random_hex();
|
||||||
|
let mut tile = HexTile::new(hex);
|
||||||
|
|
||||||
|
// Modify walls
|
||||||
|
tile.walls.remove(EdgeDirection::FLAT_TOP);
|
||||||
|
assert!(!tile.walls.contains(EdgeDirection::FLAT_TOP));
|
||||||
|
|
||||||
|
tile.walls.add(EdgeDirection::FLAT_TOP);
|
||||||
|
assert!(tile.walls.contains(EdgeDirection::FLAT_TOP));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn different_positions() {
|
fn different_positions() {
|
||||||
let positions = [Hex::ZERO, Hex::new(1, 0), Hex::new(-1, 1), Hex::new(2, -2)];
|
let positions = [Hex::ZERO, Hex::new(1, 0), Hex::new(-1, 1), Hex::new(2, -2)];
|
||||||
@ -153,7 +150,7 @@ mod test {
|
|||||||
// Create tiles at different positions
|
// Create tiles at different positions
|
||||||
let tiles = positions
|
let tiles = positions
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&pos| Tile::new(pos))
|
.map(|&pos| HexTile::new(pos))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
// Verify each tile has correct position
|
// Verify each tile has correct position
|
||||||
@ -173,7 +170,7 @@ mod test {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for pos in extreme_positions {
|
for pos in extreme_positions {
|
||||||
let tile = Tile::new(pos);
|
let tile = HexTile::new(pos);
|
||||||
assert_eq!(tile.pos, pos);
|
assert_eq!(tile.pos, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -181,7 +178,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hex_tile_creation_and_properties() {
|
fn hex_tile_creation_and_properties() {
|
||||||
let hex = random_hex();
|
let hex = random_hex();
|
||||||
let tile = Tile::new(hex);
|
let tile = HexTile::new(hex);
|
||||||
|
|
||||||
assert_eq!(tile.pos(), hex);
|
assert_eq!(tile.pos(), hex);
|
||||||
assert!(tile.walls().is_enclosed());
|
assert!(tile.walls().is_enclosed());
|
||||||
@ -190,7 +187,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hex_tile_from_hex() {
|
fn hex_tile_from_hex() {
|
||||||
let hex = random_hex();
|
let hex = random_hex();
|
||||||
let tile = Tile::from(hex);
|
let tile = HexTile::from(hex);
|
||||||
|
|
||||||
assert_eq!(tile.pos, hex);
|
assert_eq!(tile.pos, hex);
|
||||||
assert_eq!(tile.walls, Walls::default());
|
assert_eq!(tile.walls, Walls::default());
|
||||||
@ -199,7 +196,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hex_hex_into_tile() {
|
fn hex_hex_into_tile() {
|
||||||
let hex = random_hex();
|
let hex = random_hex();
|
||||||
let tile: Tile = hex.into();
|
let tile: HexTile = hex.into();
|
||||||
|
|
||||||
assert_eq!(tile.pos, hex);
|
assert_eq!(tile.pos, hex);
|
||||||
assert_eq!(tile.walls, Walls::default());
|
assert_eq!(tile.walls, Walls::default());
|
||||||
@ -207,16 +204,16 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hex_tile_display() {
|
fn hex_tile_display() {
|
||||||
let tile = Tile::new(Hex::new(3, -3));
|
let tile = HexTile::new(Hex::new(3, -3));
|
||||||
assert_eq!(format!("{tile}"), "(3,-3)");
|
assert_eq!(format!("{tile}"), "(3,-3)");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hex_tile_wall_modifications() {
|
fn hex_tile_wall_modifications() {
|
||||||
let mut tile = Tile::new(Hex::ZERO);
|
let mut tile = HexTile::new(Hex::ZERO);
|
||||||
|
|
||||||
for direction in EdgeDirection::ALL_DIRECTIONS {
|
for direction in EdgeDirection::ALL_DIRECTIONS {
|
||||||
tile.walls.insert(direction);
|
tile.walls.add(direction);
|
||||||
}
|
}
|
||||||
assert_eq!(tile.walls.count(), 6);
|
assert_eq!(tile.walls.count(), 6);
|
||||||
|
|
||||||
@ -234,17 +231,17 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hex_tile_to_vec2() {
|
fn hex_tile_to_vec2() {
|
||||||
let layout = HexLayout::default();
|
let layout = HexLayout::default();
|
||||||
let tile = Tile::new(Hex::new(1, 0));
|
let tile = HexTile::new(Hex::new(1, 0));
|
||||||
let vec2 = tile.to_vec2(&layout);
|
let vec2 = tile.to_vec2(&layout);
|
||||||
assert_eq!(vec2, Vec2::new(1.5, -0.866_025_4));
|
assert_eq!(vec2, Vec2::new(1.5, -0.8660254));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hex_tile_to_vec3() {
|
fn hex_tile_to_vec3() {
|
||||||
let layout = HexLayout::default();
|
let layout = HexLayout::default();
|
||||||
let tile = Tile::new(Hex::new(0, 1));
|
let tile = HexTile::new(Hex::new(0, 1));
|
||||||
let vec3 = tile.to_vec3(&layout);
|
let vec3 = tile.to_vec3(&layout);
|
||||||
assert_eq!(vec3, Vec3::new(0.0, 0.0, -1.732_050_8));
|
assert_eq!(vec3, Vec3::new(0.0, 0.0, -1.7320508));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,22 +0,0 @@
|
|||||||
use crate::Walls;
|
|
||||||
use hexx::Hex;
|
|
||||||
|
|
||||||
pub trait TilePosition {
|
|
||||||
/// Returns position of the tile
|
|
||||||
#[must_use]
|
|
||||||
fn pos(&self) -> Hex;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "bevy_reflect")]
|
|
||||||
pub trait WorldPositionable {
|
|
||||||
#[must_use]
|
|
||||||
fn to_vec2(&self, layout: &hexx::HexLayout) -> glam::Vec2;
|
|
||||||
#[must_use]
|
|
||||||
fn to_vec3(&self, layout: &hexx::HexLayout) -> glam::Vec3;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait WallStorage {
|
|
||||||
#[must_use]
|
|
||||||
fn walls(&self) -> &Walls;
|
|
||||||
fn walls_mut(&mut self) -> &mut Walls;
|
|
||||||
}
|
|
||||||
180
src/walls.rs
180
src/walls.rs
@ -16,15 +16,15 @@ use hexx::EdgeDirection;
|
|||||||
///
|
///
|
||||||
/// // Create a hexagon with all walls
|
/// // Create a hexagon with all walls
|
||||||
/// let walls = Walls::new();
|
/// let walls = Walls::new();
|
||||||
/// assert!(walls.is_enclosed());
|
/// assert!(walls.is_closed());
|
||||||
///
|
///
|
||||||
/// // Create a hexagon with no walls
|
/// // Create a hexagon with no walls
|
||||||
/// let mut walls = Walls::empty();
|
/// let mut walls = Walls::empty();
|
||||||
/// assert!(walls.is_empty());
|
/// assert!(walls.is_empty());
|
||||||
///
|
///
|
||||||
/// // Add specific walls
|
/// // Add specific walls
|
||||||
/// walls.insert(EdgeDirection::FLAT_NORTH);
|
/// walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
/// walls.insert(EdgeDirection::FLAT_SOUTH);
|
/// walls.add(EdgeDirection::FLAT_SOUTH);
|
||||||
/// assert_eq!(walls.count(), 2);
|
/// assert_eq!(walls.count(), 2);
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
@ -47,7 +47,7 @@ impl Walls {
|
|||||||
/// let walls = Walls::new();
|
/// let walls = Walls::new();
|
||||||
/// assert!(walls.is_enclosed());
|
/// assert!(walls.is_enclosed());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
@ -63,7 +63,7 @@ impl Walls {
|
|||||||
/// let walls = Walls::empty();
|
/// let walls = Walls::empty();
|
||||||
/// assert!(walls.is_empty());
|
/// assert!(walls.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn empty() -> Self {
|
pub const fn empty() -> Self {
|
||||||
Self(0)
|
Self(0)
|
||||||
@ -79,21 +79,17 @@ impl Walls {
|
|||||||
/// let walls = Walls::empty();
|
/// let walls = Walls::empty();
|
||||||
/// assert!(walls.is_empty());
|
/// assert!(walls.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn is_empty(&self) -> bool {
|
pub const fn is_empty(&self) -> bool {
|
||||||
self.0 == 0
|
self.0 == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert a wall in the specified direction.
|
/// Adds a wall in the specified direction.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `direction` - The direction in which to insert the wall.
|
/// - `direction` - The direction in which to add the wall.
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// Returns `true` if a wall was present, `false` otherwise.
|
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@ -103,24 +99,22 @@ impl Walls {
|
|||||||
/// let mut walls = Walls::empty();
|
/// let mut walls = Walls::empty();
|
||||||
/// assert_eq!(walls.count(), 0);
|
/// assert_eq!(walls.count(), 0);
|
||||||
///
|
///
|
||||||
/// assert!(!walls.insert(1));
|
/// walls.add(1);
|
||||||
/// assert_eq!(walls.count(), 1);
|
/// assert_eq!(walls.count(), 1);
|
||||||
///
|
///
|
||||||
/// assert!(walls.insert(1));
|
/// walls.add(1);
|
||||||
/// assert_eq!(walls.count(), 1);
|
/// assert_eq!(walls.count(), 1);
|
||||||
///
|
///
|
||||||
/// assert!(!walls.insert(EdgeDirection::FLAT_NORTH));
|
/// walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
/// assert_eq!(walls.count(), 2);
|
/// assert_eq!(walls.count(), 2);
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
pub fn insert<T>(&mut self, direction: T) -> bool
|
pub fn add<T>(&mut self, direction: T)
|
||||||
where
|
where
|
||||||
T: Into<Self>,
|
T: Into<Self> + Copy,
|
||||||
{
|
{
|
||||||
let mask = direction.into().0;
|
self.0 |= direction.into().0;
|
||||||
let was_present = self.0 & mask != 0;
|
|
||||||
self.0 |= mask;
|
|
||||||
was_present
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes a wall in the specified direction.
|
/// Removes a wall in the specified direction.
|
||||||
@ -129,10 +123,6 @@ impl Walls {
|
|||||||
///
|
///
|
||||||
/// - `direction` - The direction from which to remove the wall.
|
/// - `direction` - The direction from which to remove the wall.
|
||||||
///
|
///
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// Returns `true` if a wall was present and removed, `false` otherwise.
|
|
||||||
///
|
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
@ -149,15 +139,16 @@ impl Walls {
|
|||||||
/// assert!(walls.remove(EdgeDirection::FLAT_NORTH));
|
/// assert!(walls.remove(EdgeDirection::FLAT_NORTH));
|
||||||
/// assert_eq!(walls.count(), 4);
|
/// assert_eq!(walls.count(), 4);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
pub fn remove<T>(&mut self, direction: T) -> bool
|
pub fn remove<T>(&mut self, direction: T) -> bool
|
||||||
where
|
where
|
||||||
T: Into<Self>,
|
T: Into<Self> + Copy,
|
||||||
{
|
{
|
||||||
let mask = direction.into().0;
|
let was_removed = self.contains(direction);
|
||||||
let was_present = self.0 & mask != 0;
|
if was_removed {
|
||||||
self.0 &= !mask;
|
self.0 &= !direction.into().0;
|
||||||
was_present
|
}
|
||||||
|
was_removed
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if there is a wall in the specified direction.
|
/// Checks if there is a wall in the specified direction.
|
||||||
@ -172,17 +163,17 @@ impl Walls {
|
|||||||
/// use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
///
|
///
|
||||||
/// let mut walls = Walls::empty();
|
/// let mut walls = Walls::empty();
|
||||||
/// walls.insert(EdgeDirection::FLAT_NORTH);
|
/// walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
///
|
///
|
||||||
/// assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
/// assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
||||||
/// assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
/// assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
pub fn contains<T>(&self, direction: T) -> bool
|
pub fn contains<T>(&self, other: T) -> bool
|
||||||
where
|
where
|
||||||
T: Into<Self>,
|
T: Into<Self> + Copy,
|
||||||
{
|
{
|
||||||
self.0 & direction.into().0 != 0
|
self.0 & other.into().0 != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the raw bit representation of the walls
|
/// Returns the raw bit representation of the walls
|
||||||
@ -198,7 +189,7 @@ impl Walls {
|
|||||||
/// let walls = Walls::empty();
|
/// let walls = Walls::empty();
|
||||||
/// assert_eq!(walls.as_bits(), 0);
|
/// assert_eq!(walls.as_bits(), 0);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn as_bits(&self) -> u8 {
|
pub const fn as_bits(&self) -> u8 {
|
||||||
self.0
|
self.0
|
||||||
@ -214,13 +205,13 @@ impl Walls {
|
|||||||
/// let mut walls = Walls::empty();
|
/// let mut walls = Walls::empty();
|
||||||
/// assert!(walls.is_empty());
|
/// assert!(walls.is_empty());
|
||||||
///
|
///
|
||||||
/// walls.insert(0);
|
/// walls.add(0);
|
||||||
/// assert_eq!(walls.count(), 1);
|
/// assert_eq!(walls.count(), 1);
|
||||||
///
|
///
|
||||||
/// walls.insert(1);
|
/// walls.add(1);
|
||||||
/// assert_eq!(walls.count(), 2);
|
/// assert_eq!(walls.count(), 2);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn count(&self) -> u8 {
|
pub fn count(&self) -> u8 {
|
||||||
u8::try_from(self.0.count_ones()).unwrap_or_default()
|
u8::try_from(self.0.count_ones()).unwrap_or_default()
|
||||||
@ -235,7 +226,7 @@ impl Walls {
|
|||||||
///
|
///
|
||||||
/// assert_eq!(Walls::all_directions().as_bits(), 0b11_1111);
|
/// assert_eq!(Walls::all_directions().as_bits(), 0b11_1111);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn all_directions() -> Self {
|
pub const fn all_directions() -> Self {
|
||||||
Self(0b11_1111)
|
Self(0b11_1111)
|
||||||
@ -271,10 +262,29 @@ impl Walls {
|
|||||||
where
|
where
|
||||||
T: Into<Self> + Copy,
|
T: Into<Self> + Copy,
|
||||||
{
|
{
|
||||||
let mask = direction.into().0;
|
let is_present = self.contains(direction);
|
||||||
let was_present = self.0 & mask != 0;
|
if is_present {
|
||||||
self.0 ^= mask;
|
self.remove(direction);
|
||||||
was_present
|
} else {
|
||||||
|
self.add(direction);
|
||||||
|
}
|
||||||
|
is_present
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if walls are present in all six directions.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// `true` if the hexagon has all possible walls, making it completely enclosed.
|
||||||
|
///
|
||||||
|
/// # Deprecated
|
||||||
|
///
|
||||||
|
/// This method is deprecated since version 0.4.0. Use `is_enclosed()` instead.
|
||||||
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
|
#[must_use]
|
||||||
|
#[deprecated(since = "0.4.0", note = "use `walls::Walls::is_enclosed()`")]
|
||||||
|
pub fn is_closed(&self) -> bool {
|
||||||
|
self.is_enclosed()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if walls are present in all six directions.
|
/// Checks if walls are present in all six directions.
|
||||||
@ -294,7 +304,7 @@ impl Walls {
|
|||||||
/// walls.remove(0);
|
/// walls.remove(0);
|
||||||
/// assert!(!walls.is_enclosed());
|
/// assert!(!walls.is_enclosed());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_enclosed(&self) -> bool {
|
pub fn is_enclosed(&self) -> bool {
|
||||||
self.count() == 6
|
self.count() == 6
|
||||||
@ -307,7 +317,7 @@ impl Walls {
|
|||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// - `other` - The walls to insert, specified as a `Walls` instance or any type
|
/// - `other` - The walls to add, specified as a `Walls` instance or any type
|
||||||
/// that can be converted into `Walls`.
|
/// that can be converted into `Walls`.
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
@ -322,7 +332,7 @@ impl Walls {
|
|||||||
/// assert!(walls.contains(EdgeDirection::FLAT_SOUTH));
|
/// assert!(walls.contains(EdgeDirection::FLAT_SOUTH));
|
||||||
/// assert_eq!(walls.count(), 3);
|
/// assert_eq!(walls.count(), 3);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[cfg_attr(not(debug_assertions), inline)]
|
||||||
pub fn fill<T>(&mut self, other: T)
|
pub fn fill<T>(&mut self, other: T)
|
||||||
where
|
where
|
||||||
T: Into<Self>,
|
T: Into<Self>,
|
||||||
@ -375,7 +385,7 @@ mod test {
|
|||||||
let walls = Walls::all_directions();
|
let walls = Walls::all_directions();
|
||||||
assert!(walls.is_enclosed());
|
assert!(walls.is_enclosed());
|
||||||
assert!(!walls.is_empty());
|
assert!(!walls.is_empty());
|
||||||
assert_eq!(walls.as_bits(), 0b11_1111);
|
assert_eq!(walls.as_bits(), 0b111111);
|
||||||
}
|
}
|
||||||
|
|
||||||
// as_bits
|
// as_bits
|
||||||
@ -388,22 +398,22 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn as_bits_single_wall() {
|
fn as_bits_single_wall() {
|
||||||
let mut walls = Walls::empty();
|
let mut walls = Walls::empty();
|
||||||
walls.insert(EdgeDirection::FLAT_NORTH);
|
walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0000);
|
assert_eq!(walls.as_bits(), 0b010000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn as_bits_multiple_walls() {
|
fn as_bits_multiple_walls() {
|
||||||
let mut walls = Walls::empty();
|
let mut walls = Walls::empty();
|
||||||
walls.insert(EdgeDirection::FLAT_NORTH);
|
walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
walls.insert(EdgeDirection::FLAT_SOUTH);
|
walls.add(EdgeDirection::FLAT_SOUTH);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0010);
|
assert_eq!(walls.as_bits(), 0b010010);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn as_bits_all_walls() {
|
fn as_bits_all_walls() {
|
||||||
let walls = Walls::new();
|
let walls = Walls::new();
|
||||||
assert_eq!(walls.as_bits(), 0b11_1111);
|
assert_eq!(walls.as_bits(), 0b111111);
|
||||||
}
|
}
|
||||||
|
|
||||||
// new
|
// new
|
||||||
@ -411,7 +421,7 @@ mod test {
|
|||||||
fn new_created_closed_walls() {
|
fn new_created_closed_walls() {
|
||||||
let walls = Walls::new();
|
let walls = Walls::new();
|
||||||
assert!(walls.is_enclosed());
|
assert!(walls.is_enclosed());
|
||||||
assert_eq!(walls.as_bits(), 0b11_1111);
|
assert_eq!(walls.as_bits(), 0b111111);
|
||||||
}
|
}
|
||||||
|
|
||||||
// empty
|
// empty
|
||||||
@ -422,11 +432,11 @@ mod test {
|
|||||||
assert_eq!(walls.as_bits(), 0);
|
assert_eq!(walls.as_bits(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// insert
|
// add
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_single_wall() {
|
fn add_single_wall() {
|
||||||
let mut walls = Walls::empty();
|
let mut walls = Walls::empty();
|
||||||
walls.insert(EdgeDirection::FLAT_NORTH);
|
walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
||||||
assert_eq!(walls.count(), 1);
|
assert_eq!(walls.count(), 1);
|
||||||
}
|
}
|
||||||
@ -443,13 +453,13 @@ mod test {
|
|||||||
fn remove_nonexistent_wall() {
|
fn remove_nonexistent_wall() {
|
||||||
let mut walls = Walls::empty();
|
let mut walls = Walls::empty();
|
||||||
assert!(!walls.remove(EdgeDirection::FLAT_NORTH));
|
assert!(!walls.remove(EdgeDirection::FLAT_NORTH));
|
||||||
walls.insert(EdgeDirection::FLAT_NORTH);
|
walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
assert!(walls.remove(EdgeDirection::FLAT_NORTH));
|
assert!(walls.remove(EdgeDirection::FLAT_NORTH));
|
||||||
}
|
}
|
||||||
|
|
||||||
// toggle
|
// toggle
|
||||||
#[test]
|
#[test]
|
||||||
fn toggle_wall() {
|
fn toggle_adds_wall() {
|
||||||
let mut walls = Walls::empty();
|
let mut walls = Walls::empty();
|
||||||
assert!(!walls.toggle(EdgeDirection::FLAT_NORTH));
|
assert!(!walls.toggle(EdgeDirection::FLAT_NORTH));
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
||||||
@ -475,7 +485,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn fill_preserves_existing_walls() {
|
fn fill_preserves_existing_walls() {
|
||||||
let mut walls = Walls::empty();
|
let mut walls = Walls::empty();
|
||||||
walls.insert(EdgeDirection::FLAT_NORTH);
|
walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
walls.fill([EdgeDirection::FLAT_SOUTH, EdgeDirection::FLAT_SOUTH_EAST]);
|
walls.fill([EdgeDirection::FLAT_SOUTH, EdgeDirection::FLAT_SOUTH_EAST]);
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_SOUTH));
|
assert!(walls.contains(EdgeDirection::FLAT_SOUTH));
|
||||||
@ -520,7 +530,7 @@ mod test {
|
|||||||
fn default_creates_closed_walls() {
|
fn default_creates_closed_walls() {
|
||||||
let walls = Walls::default();
|
let walls = Walls::default();
|
||||||
assert!(walls.is_enclosed());
|
assert!(walls.is_enclosed());
|
||||||
assert_eq!(walls.as_bits(), 0b11_1111);
|
assert_eq!(walls.as_bits(), 0b111111);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -541,54 +551,54 @@ mod test {
|
|||||||
let mut walls = Walls::empty();
|
let mut walls = Walls::empty();
|
||||||
|
|
||||||
// Test single bit operations
|
// Test single bit operations
|
||||||
walls.insert(EdgeDirection::FLAT_NORTH);
|
walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0000);
|
assert_eq!(walls.as_bits(), 0b010000);
|
||||||
|
|
||||||
walls.insert(EdgeDirection::FLAT_SOUTH);
|
walls.add(EdgeDirection::FLAT_SOUTH);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0010);
|
assert_eq!(walls.as_bits(), 0b010010);
|
||||||
|
|
||||||
// Test removing middle bit
|
// Test removing middle bit
|
||||||
walls.insert(EdgeDirection::FLAT_SOUTH_EAST);
|
walls.add(EdgeDirection::FLAT_SOUTH_EAST);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0011);
|
assert_eq!(walls.as_bits(), 0b010011);
|
||||||
walls.remove(EdgeDirection::FLAT_SOUTH);
|
walls.remove(EdgeDirection::FLAT_SOUTH);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0001);
|
assert_eq!(walls.as_bits(), 0b010001);
|
||||||
}
|
}
|
||||||
|
|
||||||
// From<EdgeDirection> tests
|
// From<EdgeDirection> tests
|
||||||
#[test]
|
#[test]
|
||||||
fn from_edge_direction_flat_south_east() {
|
fn from_edge_direction_flat_south_east() {
|
||||||
let walls = Walls::from(EdgeDirection::FLAT_SOUTH_EAST);
|
let walls = Walls::from(EdgeDirection::FLAT_SOUTH_EAST);
|
||||||
assert_eq!(walls.as_bits(), 0b00_0001);
|
assert_eq!(walls.as_bits(), 0b000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_edge_direction_flat_south() {
|
fn from_edge_direction_flat_south() {
|
||||||
let walls = Walls::from(EdgeDirection::FLAT_SOUTH);
|
let walls = Walls::from(EdgeDirection::FLAT_SOUTH);
|
||||||
assert_eq!(walls.as_bits(), 0b00_0010);
|
assert_eq!(walls.as_bits(), 0b000010);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_edge_direction_flat_south_west() {
|
fn from_edge_direction_flat_south_west() {
|
||||||
let walls = Walls::from(EdgeDirection::FLAT_SOUTH_WEST);
|
let walls = Walls::from(EdgeDirection::FLAT_SOUTH_WEST);
|
||||||
assert_eq!(walls.as_bits(), 0b00_0100);
|
assert_eq!(walls.as_bits(), 0b000100);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_edge_direction_flat_north_west() {
|
fn from_edge_direction_flat_north_west() {
|
||||||
let walls = Walls::from(EdgeDirection::FLAT_NORTH_WEST);
|
let walls = Walls::from(EdgeDirection::FLAT_NORTH_WEST);
|
||||||
assert_eq!(walls.as_bits(), 0b00_1000);
|
assert_eq!(walls.as_bits(), 0b001000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_edge_direction_flat_north() {
|
fn from_edge_direction_flat_north() {
|
||||||
let walls = Walls::from(EdgeDirection::FLAT_NORTH);
|
let walls = Walls::from(EdgeDirection::FLAT_NORTH);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0000);
|
assert_eq!(walls.as_bits(), 0b010000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_edge_direction_flat_east() {
|
fn from_edge_direction_flat_east() {
|
||||||
let walls = Walls::from(EdgeDirection::FLAT_NORTH_EAST);
|
let walls = Walls::from(EdgeDirection::FLAT_NORTH_EAST);
|
||||||
assert_eq!(walls.as_bits(), 0b10_0000);
|
assert_eq!(walls.as_bits(), 0b100000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromIterator tests
|
// FromIterator tests
|
||||||
@ -603,7 +613,7 @@ mod test {
|
|||||||
let walls = vec![EdgeDirection::FLAT_SOUTH]
|
let walls = vec![EdgeDirection::FLAT_SOUTH]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Walls>();
|
.collect::<Walls>();
|
||||||
assert_eq!(walls.as_bits(), 0b00_0010);
|
assert_eq!(walls.as_bits(), 0b000010);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -611,7 +621,7 @@ mod test {
|
|||||||
let walls = vec![EdgeDirection::FLAT_NORTH, EdgeDirection::FLAT_SOUTH]
|
let walls = vec![EdgeDirection::FLAT_NORTH, EdgeDirection::FLAT_SOUTH]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Walls>();
|
.collect::<Walls>();
|
||||||
assert_eq!(walls.as_bits(), 0b01_0010);
|
assert_eq!(walls.as_bits(), 0b010010);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -623,13 +633,13 @@ mod test {
|
|||||||
]
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Walls>();
|
.collect::<Walls>();
|
||||||
assert_eq!(walls.as_bits(), 0b01_0010);
|
assert_eq!(walls.as_bits(), 0b010010);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_iterator_all_directions() {
|
fn from_iterator_all_directions() {
|
||||||
let walls = EdgeDirection::iter().collect::<Walls>();
|
let walls = EdgeDirection::iter().collect::<Walls>();
|
||||||
assert_eq!(walls.as_bits(), 0b11_1111);
|
assert_eq!(walls.as_bits(), 0b111111);
|
||||||
}
|
}
|
||||||
|
|
||||||
// From<[EdgeDirection; N]> tests
|
// From<[EdgeDirection; N]> tests
|
||||||
@ -642,13 +652,13 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn from_array_single() {
|
fn from_array_single() {
|
||||||
let walls = Walls::from([EdgeDirection::FLAT_NORTH]);
|
let walls = Walls::from([EdgeDirection::FLAT_NORTH]);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0000);
|
assert_eq!(walls.as_bits(), 0b010000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_array_multiple() {
|
fn from_array_multiple() {
|
||||||
let walls = Walls::from([EdgeDirection::FLAT_NORTH, EdgeDirection::FLAT_SOUTH]);
|
let walls = Walls::from([EdgeDirection::FLAT_NORTH, EdgeDirection::FLAT_SOUTH]);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0010);
|
assert_eq!(walls.as_bits(), 0b010010);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -658,7 +668,7 @@ mod test {
|
|||||||
EdgeDirection::FLAT_NORTH,
|
EdgeDirection::FLAT_NORTH,
|
||||||
EdgeDirection::FLAT_SOUTH,
|
EdgeDirection::FLAT_SOUTH,
|
||||||
]);
|
]);
|
||||||
assert_eq!(walls.as_bits(), 0b01_0010);
|
assert_eq!(walls.as_bits(), 0b010010);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -671,6 +681,6 @@ mod test {
|
|||||||
EdgeDirection::FLAT_SOUTH_WEST,
|
EdgeDirection::FLAT_SOUTH_WEST,
|
||||||
EdgeDirection::FLAT_NORTH_WEST,
|
EdgeDirection::FLAT_NORTH_WEST,
|
||||||
]);
|
]);
|
||||||
assert_eq!(walls.as_bits(), 0b11_1111);
|
assert_eq!(walls.as_bits(), 0b111111);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ use rstest::rstest;
|
|||||||
#[case(5, 91)]
|
#[case(5, 91)]
|
||||||
fn maze_size(#[case] radius: u16, #[case] expected_size: usize) {
|
fn maze_size(#[case] radius: u16, #[case] expected_size: usize) {
|
||||||
let maze = assert_ok!(MazeBuilder::new().with_radius(radius).build());
|
let maze = assert_ok!(MazeBuilder::new().with_radius(radius).build());
|
||||||
assert_eq!(maze.count(), expected_size);
|
assert_eq!(maze.len(), expected_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -29,7 +29,7 @@ fn valid_start_position(#[case] start_pos: Hex) {
|
|||||||
.with_radius(3)
|
.with_radius(3)
|
||||||
.with_start_position(start_pos)
|
.with_start_position(start_pos)
|
||||||
.build());
|
.build());
|
||||||
assert_some!(maze.get(&start_pos));
|
assert_some!(maze.get_tile(&start_pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -67,24 +67,28 @@ fn maze_connectivity() {
|
|||||||
let maze = assert_ok!(MazeBuilder::new().with_radius(3).build());
|
let maze = assert_ok!(MazeBuilder::new().with_radius(3).build());
|
||||||
|
|
||||||
// Helper function to count accessible neighbors
|
// Helper function to count accessible neighbors
|
||||||
let count_accessible_neighbors = |pos: Hex| -> usize {
|
fn count_accessible_neighbors(maze: &HexMaze, pos: Hex) -> usize {
|
||||||
hexx::EdgeDirection::ALL_DIRECTIONS
|
hexx::EdgeDirection::ALL_DIRECTIONS
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|&&dir| {
|
.filter(|&&dir| {
|
||||||
let neighbor = pos + dir;
|
let neighbor = pos + dir;
|
||||||
maze.get_walls(&pos)
|
if let Some(walls) = maze.get_walls(&pos) {
|
||||||
.is_some_and(|walls| !walls.contains(dir) && maze.get(&neighbor).is_some())
|
!walls.contains(dir) && maze.get_tile(&neighbor).is_some()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.count()
|
.count()
|
||||||
};
|
}
|
||||||
|
|
||||||
// Check that each tile has at least one connection
|
// Check that each tile has at least one connection
|
||||||
for &pos in maze.keys() {
|
for &pos in maze.keys() {
|
||||||
let accessible_neighbors = count_accessible_neighbors(pos);
|
let accessible_neighbors = count_accessible_neighbors(&maze, pos);
|
||||||
assert_gt!(
|
claims::assert_gt!(
|
||||||
accessible_neighbors,
|
accessible_neighbors,
|
||||||
0,
|
0,
|
||||||
"Tile at {pos:?} has no accessible neighbors",
|
"Tile at {:?} has no accessible neighbors",
|
||||||
|
pos
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,9 +96,10 @@ fn maze_connectivity() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn maze_boundaries() {
|
fn maze_boundaries() {
|
||||||
let radius = 3;
|
let radius = 3;
|
||||||
let maze = assert_ok!(MazeBuilder::new().with_radius(radius).build());
|
let maze = MazeBuilder::new()
|
||||||
|
.with_radius(radius as u16)
|
||||||
let radius = i32::from(radius);
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// Test that tiles exist within the radius
|
// Test that tiles exist within the radius
|
||||||
for q in -radius..=radius {
|
for q in -radius..=radius {
|
||||||
@ -102,8 +107,9 @@ fn maze_boundaries() {
|
|||||||
let pos = Hex::new(q, r);
|
let pos = Hex::new(q, r);
|
||||||
if q.abs() + r.abs() <= radius {
|
if q.abs() + r.abs() <= radius {
|
||||||
assert!(
|
assert!(
|
||||||
maze.get(&pos).is_some(),
|
maze.get_tile(&pos).is_some(),
|
||||||
"Expected tile at {pos:?} to exist",
|
"Expected tile at {:?} to exist",
|
||||||
|
pos
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,5 +126,5 @@ fn generate_maze_with_different_types(#[case] generator: GeneratorType) {
|
|||||||
.with_generator(generator)
|
.with_generator(generator)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
assert_gt!(maze.count(), 0);
|
assert_gt!(maze.len(), 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
use claims::assert_some;
|
|
||||||
use hexlab::prelude::*;
|
use hexlab::prelude::*;
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
|
||||||
@ -11,20 +10,20 @@ fn generator_type(
|
|||||||
#[case] start_pos: Option<Hex>,
|
#[case] start_pos: Option<Hex>,
|
||||||
#[case] seed: Option<u64>,
|
#[case] seed: Option<u64>,
|
||||||
) {
|
) {
|
||||||
let mut maze = Maze::new();
|
let mut maze = HexMaze::new();
|
||||||
for q in -3..=3 {
|
for q in -3..=3 {
|
||||||
for r in -3..=3 {
|
for r in -3..=3 {
|
||||||
let hex = Hex::new(q, r);
|
let hex = Hex::new(q, r);
|
||||||
if hex.length() <= 3 {
|
if hex.length() <= 3 {
|
||||||
maze.insert(hex);
|
maze.add_tile(hex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let initial_size = maze.count();
|
let initial_size = maze.len();
|
||||||
|
|
||||||
generator.generate(&mut maze, start_pos, seed);
|
generator.generate(&mut maze, start_pos, seed);
|
||||||
|
|
||||||
assert_eq!(maze.count(), initial_size, "Maze size should not change");
|
assert_eq!(maze.len(), initial_size, "Maze size should not change");
|
||||||
|
|
||||||
// Check maze connectivity
|
// Check maze connectivity
|
||||||
let start = start_pos.unwrap_or(Hex::ZERO);
|
let start = start_pos.unwrap_or(Hex::ZERO);
|
||||||
@ -37,27 +36,28 @@ fn generator_type(
|
|||||||
for dir in EdgeDirection::ALL_DIRECTIONS {
|
for dir in EdgeDirection::ALL_DIRECTIONS {
|
||||||
let neighbor = current + dir;
|
let neighbor = current + dir;
|
||||||
if let Some(walls) = maze.get_walls(¤t) {
|
if let Some(walls) = maze.get_walls(¤t) {
|
||||||
if !walls.contains(dir) && maze.get(&neighbor).is_some() {
|
if !walls.contains(dir) && maze.get_tile(&neighbor).is_some() {
|
||||||
to_visit.push(neighbor);
|
to_visit.push(neighbor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_eq!(visited.len(), maze.count(), "All tiles should be connected");
|
assert_eq!(visited.len(), maze.len(), "All tiles should be connected");
|
||||||
|
|
||||||
// Check that each tile has at least one open wall
|
// Check that each tile has at least one open wall
|
||||||
for &pos in maze.keys() {
|
for &pos in maze.keys() {
|
||||||
let walls = assert_some!(maze.get_walls(&pos));
|
let walls = maze.get_walls(&pos).unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
walls.count() < 6,
|
walls.count() < 6,
|
||||||
"Tile at {pos:?} should have at least one open wall",
|
"Tile at {:?} should have at least one open wall",
|
||||||
|
pos
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_empty_maze() {
|
fn test_empty_maze() {
|
||||||
let mut maze = Maze::new();
|
let mut maze = HexMaze::new();
|
||||||
GeneratorType::RecursiveBacktracking.generate(&mut maze, None, None);
|
GeneratorType::RecursiveBacktracking.generate(&mut maze, None, None);
|
||||||
assert!(
|
assert!(
|
||||||
maze.is_empty(),
|
maze.is_empty(),
|
||||||
|
|||||||
@ -1,68 +1,68 @@
|
|||||||
use claims::assert_some;
|
|
||||||
use hexlab::prelude::*;
|
use hexlab::prelude::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hex_maze_creation_and_basic_operations() {
|
fn hex_maze_creation_and_basic_operations() {
|
||||||
let mut maze = Maze::new();
|
let mut maze = HexMaze::new();
|
||||||
assert!(maze.is_empty());
|
assert!(maze.is_empty());
|
||||||
|
|
||||||
let center = Hex::ZERO;
|
let center = Hex::ZERO;
|
||||||
maze.insert(center);
|
maze.add_tile(center);
|
||||||
assert_eq!(maze.count(), 1);
|
assert_eq!(maze.len(), 1);
|
||||||
assert!(!maze.is_empty());
|
assert!(!maze.is_empty());
|
||||||
|
|
||||||
let tile = assert_some!(maze.get(¢er));
|
let tile = maze.get_tile(¢er);
|
||||||
assert_eq!(tile.pos(), center);
|
assert!(tile.is_some());
|
||||||
|
assert_eq!(tile.unwrap().pos(), center);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hex_maze_wall_operations() {
|
fn hex_maze_wall_operations() {
|
||||||
let mut maze = Maze::new();
|
let mut maze = HexMaze::new();
|
||||||
let center = Hex::ZERO;
|
let center = Hex::ZERO;
|
||||||
maze.insert(center);
|
maze.add_tile(center);
|
||||||
|
|
||||||
// Add walls
|
// Add walls
|
||||||
for direction in EdgeDirection::ALL_DIRECTIONS {
|
for direction in EdgeDirection::ALL_DIRECTIONS {
|
||||||
let _ = maze.add_tile_wall(¢er, direction);
|
maze.add_wall(center, direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
let walls = assert_some!(maze.get_walls(¢er));
|
let walls = maze.get_walls(¢er).unwrap();
|
||||||
assert_eq!(walls.count(), 6);
|
assert_eq!(walls.count(), 6);
|
||||||
|
|
||||||
// Remove walls
|
// Remove walls
|
||||||
for direction in EdgeDirection::ALL_DIRECTIONS {
|
for direction in EdgeDirection::ALL_DIRECTIONS {
|
||||||
let _ = maze.remove_tile_wall(¢er, direction);
|
maze.remove_tile_wall(¢er, direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
let walls = assert_some!(maze.get_walls(¢er));
|
let walls = maze.get_walls(¢er).unwrap();
|
||||||
assert_eq!(walls.count(), 0);
|
assert_eq!(walls.count(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hex_maze_multiple_tiles() {
|
fn hex_maze_multiple_tiles() {
|
||||||
let mut maze = Maze::new();
|
let mut maze = HexMaze::new();
|
||||||
let tiles = [Hex::ZERO, Hex::new(1, -1), Hex::new(0, 1), Hex::new(-1, 1)];
|
let tiles = [Hex::ZERO, Hex::new(1, -1), Hex::new(0, 1), Hex::new(-1, 1)];
|
||||||
|
|
||||||
for &tile in &tiles {
|
for &tile in &tiles {
|
||||||
maze.insert(tile);
|
maze.add_tile(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(maze.count(), tiles.len());
|
assert_eq!(maze.len(), tiles.len());
|
||||||
|
|
||||||
for &tile in &tiles {
|
for &tile in &tiles {
|
||||||
assert!(maze.get(&tile).is_some());
|
assert!(maze.get_tile(&tile).is_some());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hex_maze_edge_cases() {
|
fn hex_maze_edge_cases() {
|
||||||
let mut maze = Maze::new();
|
let mut maze = HexMaze::new();
|
||||||
let non_existent = Hex::new(10, 10);
|
let non_existent = Hex::new(10, 10);
|
||||||
|
|
||||||
// Operations on non-existent tiles should not panic
|
// Operations on non-existent tiles should not panic
|
||||||
let _ = maze.add_tile_wall(&non_existent, EdgeDirection::FLAT_NORTH);
|
maze.add_wall(non_existent, EdgeDirection::FLAT_NORTH);
|
||||||
let _ = maze.remove_tile_wall(&non_existent, EdgeDirection::FLAT_NORTH);
|
maze.remove_tile_wall(&non_existent, EdgeDirection::FLAT_NORTH);
|
||||||
|
|
||||||
assert!(maze.get(&non_existent).is_none());
|
assert!(maze.get_tile(&non_existent).is_none());
|
||||||
assert!(maze.get_walls(&non_existent).is_none());
|
assert!(maze.get_walls(&non_existent).is_none());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,79 +0,0 @@
|
|||||||
use claims::*;
|
|
||||||
use hexlab::MazeBuilder;
|
|
||||||
use hexx::{hex, EdgeDirection, Hex};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn basic_path() {
|
|
||||||
let maze = assert_ok!(MazeBuilder::new().with_seed(12345).with_radius(5).build());
|
|
||||||
|
|
||||||
let start = Hex::new(0, 0);
|
|
||||||
let goal = Hex::new(2, 0);
|
|
||||||
|
|
||||||
assert_some_eq!(
|
|
||||||
maze.find_path(start, goal),
|
|
||||||
vec![start, hex(1, 0), hex(1, 1), hex(2, 1), goal]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn path_with_walls() {
|
|
||||||
let mut maze = assert_ok!(MazeBuilder::new().with_seed(12345).with_radius(5).build());
|
|
||||||
let start = Hex::new(0, 0);
|
|
||||||
let goal = Hex::new(2, 0);
|
|
||||||
|
|
||||||
// Block direct path with wall
|
|
||||||
assert_ok!(maze.add_tile_wall(&start, EdgeDirection::FLAT_SOUTH));
|
|
||||||
|
|
||||||
// Should find alternative path or no path
|
|
||||||
let path = maze.find_path(start, goal);
|
|
||||||
if let Some(path) = path {
|
|
||||||
// If path exists, verify it's valid
|
|
||||||
assert!(path.len() > 3); // Should be longer than direct path
|
|
||||||
assert_eq!(path.first(), Some(&start));
|
|
||||||
assert_eq!(path.last(), Some(&goal));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn path_to_self() {
|
|
||||||
let maze = assert_ok!(MazeBuilder::new().with_seed(12345).with_radius(5).build());
|
|
||||||
let pos = Hex::new(0, 0);
|
|
||||||
|
|
||||||
assert_some_eq!(maze.find_path(pos, pos), vec![pos]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn no_path_exists() {
|
|
||||||
let mut maze = assert_ok!(MazeBuilder::new().with_seed(12345).with_radius(5).build());
|
|
||||||
let start = Hex::new(0, 0);
|
|
||||||
let goal = Hex::new(2, 0);
|
|
||||||
|
|
||||||
// Surround start with walls
|
|
||||||
for dir in EdgeDirection::ALL_DIRECTIONS {
|
|
||||||
assert_ok!(maze.add_tile_wall(&start, dir));
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_none!(maze.find_path(start, goal));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn path_in_larger_maze() {
|
|
||||||
let maze = assert_ok!(MazeBuilder::new().with_seed(12345).with_radius(10).build());
|
|
||||||
let start = Hex::new(-5, -5);
|
|
||||||
let goal = Hex::new(5, 5);
|
|
||||||
|
|
||||||
let path = assert_some!(maze.find_path(start, goal));
|
|
||||||
|
|
||||||
// Basic path properties
|
|
||||||
assert_eq!(path.first(), Some(&start));
|
|
||||||
assert_eq!(path.last(), Some(&goal));
|
|
||||||
|
|
||||||
// Path should be continuous
|
|
||||||
for window in path.windows(2) {
|
|
||||||
let current = window[0];
|
|
||||||
let next = window[1];
|
|
||||||
assert!(EdgeDirection::ALL_DIRECTIONS
|
|
||||||
.iter()
|
|
||||||
.any(|&dir| current.neighbor(dir) == next));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user