Skip to content

Commit

Permalink
chore: adjust path finding
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed May 28, 2024
1 parent 305b40c commit 6da4c32
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions battle_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ pub const CAN_CROUCH_AFTER: u64 = TARGET_FPS * 60 * 5;
// How many frames after last proximity shoot needed before soldier go from crouch to standup when idle
pub const CAN_STANDUP_AFTER: u64 = TARGET_FPS * 60 * 10;

// Adjust distance (computed by pixels) according to tile cost
pub const PATH_FINDING_HEURISTIC_COEFFICIENT: f32 = 10.;

#[derive(Debug, Clone)]
pub struct ServerConfig {
pub send_debug_points: bool,
Expand Down Expand Up @@ -155,6 +158,7 @@ pub struct ServerConfig {
pub explosive_regressive_death_rayon: HashMap<ExplosiveType, Distance>,
pub explosive_regressive_injured_rayon: HashMap<ExplosiveType, Distance>,
pub hide_maximum_rayon: Distance,
pub path_finding_heuristic_coefficient: f32,
}

impl Default for ServerConfig {
Expand Down Expand Up @@ -241,6 +245,7 @@ impl Default for ServerConfig {
explosive_regressive_injured_rayon,

hide_maximum_rayon: Distance::from_meters(HIDE_MAXIMUM_RAYON),
path_finding_heuristic_coefficient: PATH_FINDING_HEURISTIC_COEFFICIENT,
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions battle_core/src/physics/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{map::Map, types::*, utils::angleg};
use crate::{config::ServerConfig, map::Map, types::*, utils::angleg};
use pathfinding::prelude::astar;
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
Expand Down Expand Up @@ -157,6 +157,7 @@ impl Direction {

// TODO : When "to" is unreachable (ex. for vehicle) do not search a path (it consume all path before stop)
pub fn find_path(
config: &ServerConfig,
map: &Map,
from: &GridPoint,
to: &GridPoint,
Expand All @@ -172,7 +173,10 @@ pub fn find_path(
match astar(
&(*from, start_direction),
|p| map.successors(p, path_mode),
|p| (p.0.x.abs_diff(to.x) + p.0.y.abs_diff(to.y)) as i32,
|p| {
(p.0.to_vec2().distance(to.to_vec2()) * config.path_finding_heuristic_coefficient)
as i32
},
|p| p.0 == *to,
) {
None => None,
Expand Down
2 changes: 1 addition & 1 deletion battle_core/src/physics/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::{Distance, WorldPoint};
use crate::types::{Distance, GridPoint, WorldPoint};

// Coefficient to convert distance from two scene points into meters
// TODO : fix it with sprites, maps, etc
Expand Down
4 changes: 4 additions & 0 deletions battle_core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ impl GridPoint {
pub fn new(x: i32, y: i32) -> Self {
Self { x, y }
}

pub fn to_vec2(&self) -> Vec2 {
Vec2::new(self.x as f32, self.y as f32)
}
}

impl Display for GridPoint {
Expand Down

0 comments on commit 6da4c32

Please sign in to comment.