Skip to content

Commit

Permalink
Switched from RefCell to Arc<Mutex> to make PathingGrid Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
tbvanderwoude committed Jan 3, 2025
1 parent 0d81ec5 commit 328d3b4
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
//! to avoid flood-filling behaviour if no path exists.
mod astar_jps;
use astar_jps::AstarContext;
use core::fmt;
use grid_util::direction::Direction;
use grid_util::grid::{BoolGrid, Grid, SimpleGrid};
use grid_util::point::Point;
use petgraph::unionfind::UnionFind;
use smallvec::{smallvec, SmallVec};

use crate::astar_jps::astar_jps;
use core::fmt;
use std::cell::RefCell;
use smallvec::SmallVec;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

const EQUAL_EDGE_COST: bool = false;
const GRAPH_PRUNING: bool = true;
Expand Down Expand Up @@ -72,7 +70,7 @@ pub struct PathingGrid {
pub heuristic_factor: f32,
pub improved_pruning: bool,
pub allow_diagonal_move: bool,
context: RefCell<AstarContext<Point, i32>>,
context: Arc<Mutex<AstarContext<Point, i32>>>,
}

impl Default for PathingGrid {
Expand All @@ -85,7 +83,7 @@ impl Default for PathingGrid {
improved_pruning: true,
heuristic_factor: 1.0,
allow_diagonal_move: true,
context: AstarContext::new().into(),
context: Arc::new(Mutex::new(AstarContext::new())),
}
}
}
Expand Down Expand Up @@ -396,7 +394,7 @@ impl PathingGrid {
if goals.is_empty() {
return None;
}
let mut ct = self.context.borrow_mut();
let mut ct = self.context.lock().unwrap();
let result = ct.astar_jps(
&start,
|parent, node| {
Expand Down Expand Up @@ -432,7 +430,7 @@ impl PathingGrid {
return None;
}
// A neighbour of the goal can be reached, compute a path
let mut ct = self.context.borrow_mut();
let mut ct = self.context.lock().unwrap();
ct.astar_jps(
&start,
|parent, node| {
Expand All @@ -453,7 +451,7 @@ impl PathingGrid {
return None;
}
// The goal is reachable from the start, compute a path
let mut ct = self.context.borrow_mut();
let mut ct = self.context.lock().unwrap();
ct.astar_jps(
&start,
|parent, node| {
Expand Down Expand Up @@ -553,7 +551,7 @@ impl Grid<bool> for PathingGrid {
improved_pruning: true,
heuristic_factor: 1.0,
allow_diagonal_move: true,
context: AstarContext::new().into(),
context: Arc::new(Mutex::new(AstarContext::new())),
};
// Emulates 'placing' of blocked tile around map border to correctly initialize neighbours
// and make behaviour of a map bordered by tiles the same as a borderless map.
Expand Down

0 comments on commit 328d3b4

Please sign in to comment.