com_croftsoft_core/ai/astar/structures/
mod.rs

1// =============================================================================
2//! - Structures for the A* algorithm
3//!
4//! # Metadata
5//! - Copyright: © 2022-2023 [`CroftSoft Inc`]
6//! - Author: [`David Wallace Croft`]
7//! - Java created: 2002-04-21
8//! - Java updated: 2003-05-10
9//! - Rust created: 2022-10-22
10//! - Rust updated: 2023-07-23
11//!
12//! # History
13//! - Adapted from the classes in the Java-based [`CroftSoft Core Library`]
14//!   - com.croftsoft.core.ai.astar.GridCartographer
15//!   - com.croftsoft.core.ai.astar.NodeInfo
16//!
17//! [`CroftSoft Core Library`]: https://www.croftsoft.com/library/code/
18//! [`CroftSoft Inc`]: https://www.croftsoft.com/
19//! [`David Wallace Croft`]: https://www.croftsoft.com/people/david/
20// =============================================================================
21
22use super::types::{IsSpaceAvailableFunction, MakeNodeFunction};
23use core::hash::Hash;
24use std::collections::HashMap;
25use std::collections::VecDeque;
26
27pub struct AStar<N: Eq + Hash> {
28  pub best_node_option: Option<N>,
29  pub best_total_cost: f64,
30  pub goal_node_option: Option<N>,
31  pub list_empty: bool,
32  pub node_to_node_info_map: HashMap<N, NodeInfo>,
33  pub node_to_parent_node_map: HashMap<N, N>,
34  pub open_node_sorted_list: VecDeque<N>,
35}
36
37/// Gradient cartographer for continuous space.
38/// The adjacent nodes are spaced farther apart as you move away from the
39/// starting point.
40pub struct GradientCartographer<N> {
41  pub directions: u64,
42  pub goal_node: N,
43  pub init_step_size: f64,
44  pub is_space_available_fn: IsSpaceAvailableFunction<N>,
45  pub make_node_fn: MakeNodeFunction<N>,
46  pub start_node: N,
47}
48
49/// Grid cartographer for continuous space.
50/// The nodes are spaced equally apart in the eight cardinal directions.
51pub struct GridCartographer<N> {
52  pub goal_node: N,
53  pub is_space_available_fn: IsSpaceAvailableFunction<N>,
54  pub make_node_fn: MakeNodeFunction<N>,
55  pub step_size: f64,
56}
57
58#[derive(Clone, Copy, Debug, Default)]
59/// A* algorithm node information
60pub struct NodeInfo {
61  pub cost_from_start: f64,
62  pub total_cost: f64,
63}