aboutsummaryrefslogtreecommitdiff
path: root/server/bot/src/pathfinding.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/bot/src/pathfinding.rs')
-rw-r--r--server/bot/src/pathfinding.rs24
1 files changed, 17 insertions, 7 deletions
diff --git a/server/bot/src/pathfinding.rs b/server/bot/src/pathfinding.rs
index a32442b1..e17ca80c 100644
--- a/server/bot/src/pathfinding.rs
+++ b/server/bot/src/pathfinding.rs
@@ -15,11 +15,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+use hurrycurry_game_core::Game;
use hurrycurry_protocol::glam::{IVec2, Vec2};
use log::{debug, trace};
use std::{
cmp::Ordering,
- collections::{BinaryHeap, HashMap, HashSet},
+ collections::{BinaryHeap, HashMap},
time::Instant,
};
@@ -54,19 +55,19 @@ impl Path {
}
}
-pub fn find_path_to_neighbour(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Path> {
+pub fn find_path_to_neighbour(game: &Game, from: IVec2, to: IVec2) -> Option<Path> {
let mut paths = Vec::new();
for xo in -1..=1 {
for yo in -1..=1 {
let to = to + IVec2::new(xo, yo);
- if walkable.contains(&to) {
- paths.extend(find_path(walkable, from, to))
+ if game.walkable.contains(&to) {
+ paths.extend(find_path(game, from, to))
}
}
}
paths.into_iter().min_by_key(|p| p.segments.len())
}
-pub fn find_path(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Path> {
+pub fn find_path(game: &Game, from: IVec2, to: IVec2) -> Option<Path> {
#[derive(Debug, PartialEq, Eq)]
struct Open {
heuristic: i32,
@@ -87,6 +88,7 @@ pub fn find_path(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Pa
debug!("planning route from {from} to {to}");
let start = Instant::now();
+ let chair = game.data.get_tile_by_name("chair");
let mut visited = HashMap::new();
let mut open = BinaryHeap::new();
open.push(Open {
@@ -112,12 +114,20 @@ pub fn find_path(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Pa
}
for dir in [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y] {
let next = pos + dir;
- if walkable.contains(&next) {
+ if game.walkable.contains(&next) {
+ let penalty = if let Some(chair) = chair
+ && let Some(set) = game.tile_index.get(&chair)
+ && set.contains(&next)
+ {
+ 8
+ } else {
+ 0
+ };
open.push(Open {
heuristic: -(distance + next.distance_squared(to).isqrt()),
pos: next,
prev_pos: pos,
- distance: distance + 1,
+ distance: distance + penalty + 1,
});
}
}