-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3f2f42a
Showing
8 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "labrat" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 eboatwright | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# labrat.rs | ||
A simple grid-based A* implementation in Rust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pub mod node; | ||
pub mod pathfinder; | ||
|
||
pub use node::*; | ||
pub use pathfinder::*; | ||
|
||
pub(crate) fn distance(a: (f32, f32), b: (f32, f32)) -> f32 { | ||
f32::sqrt(f32::powf(a.0 - b.0, 2.0) + f32::powf(a.1 - b.1, 2.0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#[derive(Copy, Clone, Debug, PartialEq)] | ||
pub struct Node { | ||
pub position: (f32, f32), | ||
pub from: usize, | ||
|
||
pub g: u32, | ||
pub h: u32, | ||
} | ||
|
||
impl Node { | ||
pub fn x(&self) -> usize { | ||
self.position.0 as usize | ||
} | ||
|
||
pub fn y(&self) -> usize { | ||
self.position.1 as usize | ||
} | ||
|
||
pub fn f(&self) -> u32 { | ||
self.g + self.h | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use crate::*; | ||
|
||
pub struct Pathfinder { | ||
pub start: (f32, f32), | ||
pub end: (f32, f32), | ||
|
||
pub world: Vec<Vec<usize>>, | ||
|
||
pub nodes: Vec<Node>, | ||
|
||
pub path: Vec<(f32, f32)>, | ||
pub found_path: bool, | ||
} | ||
|
||
impl Pathfinder { | ||
pub fn new(start: (f32, f32), end: (f32, f32), world: Vec<Vec<usize>>) -> Self { | ||
Self { | ||
start, | ||
end, | ||
|
||
world, | ||
|
||
nodes: vec![], | ||
|
||
path: vec![], | ||
found_path: false, | ||
} | ||
} | ||
|
||
fn get_neighbors(&mut self, node: usize) -> Vec<usize> { | ||
let mut result = vec![]; | ||
|
||
let position = self.nodes[node].position; | ||
for y in (position.1 - 1.0) as usize..=(position.1 + 1.0) as usize { | ||
for x in (position.0 - 1.0) as usize..=(position.0 + 1.0) as usize { | ||
let neighbor_position = (x as f32, y as f32); | ||
if neighbor_position == position | ||
|| y >= self.world.len() | ||
|| x >= self.world[0].len() | ||
|| self.world[y][x] == 1 { | ||
continue; | ||
} | ||
|
||
let mut already_created = false; | ||
for i in 0..self.nodes.len() { | ||
if self.nodes[i].position == neighbor_position { | ||
result.push(i); | ||
already_created = true; | ||
break; | ||
} | ||
} | ||
|
||
if !already_created { | ||
self.nodes.push( | ||
Node { | ||
position: neighbor_position, | ||
from: node, | ||
|
||
g: (distance(neighbor_position, self.start) * 10.0) as u32, | ||
h: (distance(neighbor_position, self.end) * 10.0) as u32, | ||
} | ||
); | ||
|
||
result.push(self.nodes.len() - 1); | ||
} | ||
} | ||
} | ||
|
||
result | ||
} | ||
|
||
pub fn find_path(&mut self) { | ||
let mut open: Vec<usize> = vec![]; | ||
let mut closed: Vec<usize> = vec![]; | ||
|
||
self.nodes.push( | ||
Node { | ||
position: self.start, | ||
from: 0, | ||
|
||
g: 0, | ||
h: (distance(self.start, self.end) * 10.0) as u32, | ||
} | ||
); | ||
|
||
open.push(0); | ||
|
||
while open.len() > 0 { | ||
let mut current = usize::MAX; | ||
for i in open.iter() { | ||
if current == usize::MAX | ||
|| self.nodes[*i].f() < self.nodes[current].f() { | ||
current = *i; | ||
} | ||
} | ||
|
||
open.retain(|x| *x != current); | ||
closed.push(current); | ||
|
||
if self.nodes[current].position == self.end { | ||
self.found_path = true; | ||
|
||
while self.nodes[current].position != self.start { | ||
self.path.push(self.nodes[current].position); | ||
current = self.nodes[current].from; | ||
} | ||
|
||
self.path.reverse(); | ||
|
||
return; | ||
} | ||
|
||
for neighbor in self.get_neighbors(current) { | ||
if closed.contains(&neighbor) { | ||
continue; | ||
} | ||
|
||
let new_movement_cost = self.nodes[current].g + (distance(self.nodes[current].position, self.nodes[neighbor].position) * 10.0) as u32; | ||
if new_movement_cost < self.nodes[neighbor].g | ||
|| !open.contains(&neighbor) { | ||
self.nodes[neighbor].g = new_movement_cost; | ||
self.nodes[neighbor].from = current; | ||
|
||
if !open.contains(&neighbor) { | ||
open.push(neighbor); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |