Crates.io | movingai |
lib.rs | movingai |
version | 1.3.1 |
source | src |
created_at | 2018-01-24 14:51:43.893795 |
updated_at | 2024-04-26 12:29:57.995697 |
description | MovingAI Benchmark Map/Scen File Parser |
homepage | https://github.com/THeK3nger/movingai-rust |
repository | https://github.com/THeK3nger/movingai-rust |
max_upload_size | |
id | 48152 |
size | 834,930 |
Map/Scenario Parser for the MovingAI benchmark format. It offers a quick way to parse scenario and map files, with the addition of some utilities to manage and query information from the maps.
The crate parses map and scene files and provides several function for easy interaction and query.
Some of the functionalities are:
.map
files..map
and .scen
files into JSON/YAML using serde (activate --features serde
)extern crate movingai;
use std::path::Path;
use movingai::parser::parse_map_file;
fn main() {
let map = parse_map_file(Path::new("./test/arena.map")).unwrap();
let width = map.width();
let tile = map[(4,5)]; // Access map location at row 4 and column 5.
}
As an example, you can see how we can use this crate to easily implement the A* pathfinding algorithm.
// A* shortest path algorithm.
fn shortest_path(map: &MovingAiMap, start: Coords2D, goal: Coords2D) -> Option<f64> {
let mut heap = BinaryHeap::new();
let mut visited = Vec::<Coords2D>::new();
// We're at `start`, with a zero cost
heap.push(SearchNode { f: 0.0, g:0.0, h: distance(start, goal), current: start });
while let Some(SearchNode { f: _f, g, h: _h, current }) = heap.pop() {
if current == goal { return Some(g); }
if visited.contains(¤t) {
continue;
}
visited.push(current);
for neigh in map.neighbors(current) {
let new_h = distance(neigh, goal);
let i = distance(neigh, current);
let next = SearchNode { f: g+i+new_h, g: g+i, h: new_h, current: neigh };
heap.push(next);
}
}
// Goal not reachable
None
}
And in this example we can see how to write a benchmark over a scen file.
fn main() {
let map = parse_map_file(Path::new("./tests/arena.map")).unwrap();
let scenes = parse_scen_file(Path::new("./tests/arena.map.scen")).unwrap();
for scene in scenes {
let start = scene.start_pos;
let goal = scene.goal_pos;
let t = Instant::now();
match shortest_path(&map, (1,3), (4,3)) {
Some(x) => {
let duration = t.elapsed();
let seconds = duration.as_secs();
let ms = (duration.subsec_nanos() as f64) / 1_000_000.0;
println!("{:?} -> {:?} \tin {:?} seconds and {:?} ms", start, goal, seconds, ms);
}
None => println!("None"),
}
}
}
cargo test
is failing?Note that tests need to be compiled with the serde
feature enabled.
cargo test --feature=serde