blackout/src/pathfinding.rs

422 lines
14 KiB
Rust
Raw Normal View History

use bevy::{
2022-01-21 00:06:20 +00:00
ecs::entity::Entities,
prelude::*,
tasks::{prelude::*, Task},
utils::HashMap,
};
use bevy_rapier2d::{
2022-05-06 16:07:59 +00:00
na::{Isometry2, Vector2},
prelude::*,
2022-07-12 17:43:43 +00:00
rapier::prelude::{ColliderHandle, ColliderSet, QueryPipeline, RigidBodySet},
};
use futures_lite::future;
use leafwing_input_manager::{axislike::DualAxisData, plugin::InputManagerSystem, prelude::*};
2021-05-13 17:25:45 +00:00
use pathfinding::prelude::*;
use crate::{
2023-03-28 13:49:17 +00:00
core::{PointLike, TransformExt},
map::{Map, MapObstruction},
navigation::{NavigationAction, RotationSpeed},
2021-05-13 17:25:45 +00:00
};
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
pub struct NegotiatePathAction;
impl Actionlike for NegotiatePathAction {
2023-02-01 20:08:06 +00:00
fn n_variants() -> usize {
1
}
fn get_at(index: usize) -> Option<Self> {
if index == 0 {
Some(Self)
} else {
None
}
}
fn index(&self) -> usize {
0
}
}
2022-01-10 19:50:52 +00:00
#[derive(Component, Debug, Deref, DerefMut)]
struct Calculating(Task<Option<Path>>);
2022-01-10 19:50:52 +00:00
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Eq, Hash, PartialEq, Reflect)]
2021-05-13 17:25:45 +00:00
#[reflect(Component)]
pub struct Destination(pub (i32, i32));
impl_pointlike_for_tuple_component!(Destination);
impl_pointlike_for_tuple_component!(&Destination);
2021-05-13 17:25:45 +00:00
2022-01-10 19:50:52 +00:00
#[derive(Component, Clone, Debug, Default, Reflect)]
2021-07-27 16:17:06 +00:00
#[reflect(Component)]
pub struct NoPath;
2022-01-10 19:50:52 +00:00
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
2021-05-13 17:25:45 +00:00
#[reflect(Component)]
pub struct Path(pub Vec<(i32, i32)>);
#[derive(Component, Clone, Debug, Default, Reflect, Deref, DerefMut)]
#[reflect(Component)]
pub struct CostMap(pub HashMap<(i32, i32), f32>);
2022-03-15 15:37:28 +00:00
pub fn find_path<D: 'static + Clone + Default + Send + Sync>(
2021-05-13 17:25:45 +00:00
start: &dyn PointLike,
destination: &dyn PointLike,
2022-03-15 15:37:28 +00:00
map: &Map<D>,
cost_map: &Option<CostMap>,
2021-05-13 17:25:45 +00:00
) -> Option<(Vec<(i32, i32)>, u32)> {
astar(
&start.into(),
|p| {
let mut successors: Vec<((i32, i32), u32)> = vec![];
if p.0 >= 0 && p.1 >= 0 {
if let Some(tile) = map.at(p.0 as usize, p.1 as usize) {
if tile.is_walkable() {
for tile in map.get_available_exits(p.0 as usize, p.1 as usize) {
let mut cost = tile.2 * 100.;
if let Some(cost_map) = cost_map {
if let Some(modifier) =
cost_map.get(&(tile.0 as i32, tile.1 as i32))
{
cost *= modifier;
}
}
successors.push(((tile.0 as i32, tile.1 as i32), cost as u32));
}
2022-08-27 14:11:37 +00:00
}
2021-09-16 19:26:45 +00:00
}
2021-05-13 17:25:45 +00:00
}
successors
},
|p| (p.distance_squared(destination) * 100.) as u32,
|p| *p == destination.into(),
)
}
fn find_path_for_shape(
2022-05-06 16:07:59 +00:00
initiator: ColliderHandle,
2022-05-10 18:56:49 +00:00
start: Transform,
destination: Destination,
cost_map: &Option<CostMap>,
2022-05-06 16:07:59 +00:00
query_pipeline: QueryPipeline,
collider_set: ColliderSet,
2022-07-12 17:43:43 +00:00
rigid_body_set: RigidBodySet,
2022-05-06 16:07:59 +00:00
shape: Collider,
) -> Option<Path> {
let path = astar(
&start.i32(),
|p| {
let mut successors: Vec<((i32, i32), u32)> = vec![];
2022-12-19 20:08:31 +00:00
let x = p.0;
let y = p.1;
let exits = vec![
((x - 1, y), 1.),
((x + 1, y), 1.),
((x, y - 1), 1.),
((x, y + 1), 1.),
((x - 1, y - 1), 1.5),
((x + 1, y - 1), 1.5),
((x - 1, y + 1), 1.5),
((x + 1, y + 1), 1.5),
];
2023-03-28 13:49:17 +00:00
let pos = Vector2::new(x as f32, y as f32);
let shape_pos = Isometry2::new(pos, 0.);
for exit in &exits {
let mut should_push = true;
2023-03-28 13:49:17 +00:00
let dest = Vector2::new(exit.0 .0 as f32, exit.0 .1 as f32);
let shape_vel = dest - pos;
let max_toi = 1.;
if query_pipeline
.cast_shape(
&rigid_body_set,
&collider_set,
&shape_pos,
&shape_vel,
&*shape.raw,
max_toi,
true,
bevy_rapier2d::rapier::pipeline::QueryFilter::new()
.predicate(&|h, _c| h != initiator),
)
.is_some()
{
should_push = false;
}
if should_push {
let mut cost = exit.1 * 100.;
if let Some(cost_map) = cost_map {
if let Some(modifier) = cost_map.get(&(exit.0 .0, exit.0 .1)) {
cost *= modifier;
2022-08-27 14:11:37 +00:00
}
2021-09-16 19:26:45 +00:00
}
successors.push((exit.0, cost as u32));
}
}
successors
},
|p| (p.distance_squared(&destination) * 100.) as u32,
|p| *p == destination.i32(),
);
if let Some(path) = path {
Some(Path(path.0))
} else {
None
}
}
fn calculate_path(
2021-05-13 17:25:45 +00:00
mut commands: Commands,
2022-05-06 16:07:59 +00:00
rapier_context: Res<RapierContext>,
obstructions: Query<&RapierColliderHandle, With<MapObstruction>>,
2022-01-10 19:50:52 +00:00
query: Query<
2022-05-06 16:07:59 +00:00
(
Entity,
&RapierColliderHandle,
&Destination,
2022-05-10 18:56:49 +00:00
&Transform,
2022-05-06 16:07:59 +00:00
&Collider,
Option<&CostMap>,
2022-05-06 16:07:59 +00:00
),
2022-01-10 19:50:52 +00:00
Changed<Destination>,
>,
2021-05-13 17:25:45 +00:00
) {
for (entity, handle, destination, coordinates, shape, cost_map) in &query {
2021-07-21 20:20:02 +00:00
if coordinates.i32() == **destination {
2021-07-27 16:17:06 +00:00
commands
.entity(entity)
.remove::<Path>()
2021-07-28 01:17:36 +00:00
.remove::<NoPath>()
.remove::<Calculating>()
.remove::<Destination>();
2021-07-21 20:20:02 +00:00
continue;
}
2023-03-28 13:49:17 +00:00
trace!(
"Calculating path from {:?}",
coordinates.translation.truncate().i32()
);
let coordinates_clone = *coordinates;
let destination_clone = *destination;
let query_pipeline = rapier_context.query_pipeline.clone();
let cost_map_clone = cost_map.cloned();
let handle_clone = *handle;
let mut collider_set = rapier_context.colliders.clone();
let mut to_remove = vec![];
for handle in collider_set.iter() {
if !obstructions.iter().map(|v| v.0).any(|x| x == handle.0) {
to_remove.push(handle.0);
}
}
let mut bodies = rapier_context.bodies.clone();
if !to_remove.is_empty() {
let mut islands = rapier_context.islands.clone();
for handle in to_remove {
collider_set.remove(handle, &mut islands, &mut bodies, false);
}
2022-01-26 20:56:52 +00:00
}
let shape_clone = (*shape).clone();
trace!(
2023-03-28 13:49:17 +00:00
"{entity:?}: path: calculating from {:?} to {destination:?}",
coordinates.i32(),
);
let pool = AsyncComputeTaskPool::get();
let task = pool.spawn(async move {
find_path_for_shape(
handle_clone.0,
coordinates_clone,
destination_clone,
&cost_map_clone,
query_pipeline,
collider_set,
bodies,
shape_clone,
)
});
2023-04-01 12:20:59 +00:00
commands
.entity(entity)
.insert(Calculating(task))
.remove::<Path>()
.remove::<NoPath>();
}
}
2021-07-28 01:26:16 +00:00
fn poll_tasks(mut commands: Commands, mut query: Query<(Entity, &mut Calculating)>) {
2023-03-28 17:13:23 +00:00
for (entity, mut calculating) in &mut query {
2021-07-28 01:26:16 +00:00
if let Some(result) = future::block_on(future::poll_once(&mut **calculating)) {
if let Some(path) = result {
2023-03-28 13:49:17 +00:00
trace!("{entity:?}: Path: {path:?}");
2021-07-28 01:26:16 +00:00
commands.entity(entity).insert(path);
} else {
2023-03-28 13:49:17 +00:00
trace!("{entity:?}: path: no path");
2021-07-28 01:26:16 +00:00
commands.entity(entity).insert(NoPath);
}
commands.entity(entity).remove::<Calculating>();
2021-05-13 17:25:45 +00:00
}
}
}
2022-01-21 00:06:20 +00:00
fn remove_destination(
mut commands: Commands,
entities: &Entities,
2023-03-28 16:57:37 +00:00
mut removed: RemovedComponents<Destination>,
2022-01-21 00:06:20 +00:00
) {
2023-03-28 16:57:37 +00:00
for entity in &mut removed {
2022-01-21 00:06:20 +00:00
if entities.contains(entity) {
commands.entity(entity).remove::<Calculating>();
}
2021-07-28 01:26:16 +00:00
}
}
2021-06-15 22:48:49 +00:00
fn negotiate_path(
2021-05-13 17:25:45 +00:00
mut commands: Commands,
mut query: Query<(
Entity,
&mut ActionState<NavigationAction>,
2021-05-13 17:25:45 +00:00
&mut Path,
2022-05-06 16:07:59 +00:00
&mut Transform,
2023-03-28 13:49:17 +00:00
&Collider,
2021-05-13 17:25:45 +00:00
Option<&RotationSpeed>,
)>,
2023-03-28 13:49:17 +00:00
rapier_context: Res<RapierContext>,
obstructions: Query<&MapObstruction>,
2021-05-13 17:25:45 +00:00
) {
2023-03-28 13:49:17 +00:00
for (entity, mut actions, mut path, mut transform, collider, rotation_speed) in &mut query {
let start_i32 = transform.translation.truncate().i32();
trace!(
"{entity:?}: start pathfinding from {start_i32:?} to {:?}: {:?}",
path.last(),
transform.translation.truncate()
);
2023-03-28 13:49:17 +00:00
if path.len() > 0 {
if path.len() > 1 && path[0] != start_i32 && path[1] != start_i32 {
trace!("Off path");
} else if path.len() > 1 && path[1] == start_i32 {
trace!("Next in path is start of path, shouldn't happen");
}
if path[0] == start_i32 {
trace!("At start, removing");
path.remove(0);
2021-05-13 17:25:45 +00:00
}
}
2023-03-28 13:49:17 +00:00
if let Some(next) = path.first() {
trace!("{entity:?}: path: moving from {start_i32:?} to {next:?}");
let start = transform.translation.truncate();
let mut next = Vec2::new(next.0 as f32, next.1 as f32);
if next.x > 0. {
next.x += 0.5;
} else {
next -= 0.5;
}
2023-03-28 13:49:17 +00:00
if next.y > 0. {
next.y += 0.5;
} else {
2023-03-28 13:49:17 +00:00
next.y -= 0.5;
}
2023-03-28 13:49:17 +00:00
let mut direction = next - start;
direction = direction.normalize();
trace!(
"{entity:?}: Direction: {direction:?}, Distance: {}",
(next - start).length()
);
2023-04-15 14:46:34 +00:00
if let Some((_hit, _toi)) = rapier_context.cast_shape(
2023-03-28 13:49:17 +00:00
start,
transform.yaw().radians(),
direction,
2023-03-28 16:57:37 +00:00
collider,
2023-03-28 13:49:17 +00:00
rapier_context.integration_parameters.dt,
QueryFilter::new()
.predicate(&|entity| obstructions.get(entity).is_ok())
2023-03-28 13:49:17 +00:00
.exclude_sensors()
.exclude_collider(entity),
) {
2023-04-10 20:25:00 +00:00
// println!("{entity:?} is stuck, hit: {hit:?}, TOI: {toi:?}");
2023-03-28 13:49:17 +00:00
// TODO: Remove when we have an actual character controller.
transform.translation = next.extend(0.);
continue;
}
trace!("{entity:?}: path: direction: {direction:?}");
actions.press(NavigationAction::Move);
actions.action_data_mut(NavigationAction::Move).axis_pair =
Some(DualAxisData::from_xy(Vec2::new(-direction.y, direction.x)));
if rotation_speed.is_some() {
let angle = direction.y.atan2(direction.x);
transform.rotation = Quat::from_rotation_z(angle);
}
} else {
trace!("{entity:?}: empty path, cleaning");
2021-07-27 16:17:06 +00:00
commands
.entity(entity)
.remove::<Path>()
2021-09-16 19:26:45 +00:00
.remove::<NoPath>()
.remove::<Destination>();
actions.release(NavigationAction::Move);
trace!("{entity:?}: pathfinding: cleaned up");
}
}
}
fn actions(
mut commands: Commands,
mut query: Query<(
Entity,
2023-03-28 13:49:17 +00:00
&mut ActionState<NegotiatePathAction>,
&mut ActionState<NavigationAction>,
Option<&mut Destination>,
)>,
) {
2023-03-28 13:49:17 +00:00
for (entity, mut actions, mut navigation_action, destination) in &mut query {
if actions.pressed(NegotiatePathAction) {
if let Some(pair) = actions.axis_pair(NegotiatePathAction) {
2023-03-28 13:49:17 +00:00
trace!("Negotiating path to {pair:?}");
let dest = Destination((pair.x() as i32, pair.y() as i32));
if let Some(mut current_dest) = destination {
if *current_dest != dest {
2023-03-28 13:49:17 +00:00
trace!("{entity:?}: New destination, zeroing velocity");
navigation_action.press(NavigationAction::SetLinearVelocity);
navigation_action
.action_data_mut(NavigationAction::SetLinearVelocity)
.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
*current_dest = dest;
}
} else {
2023-03-28 13:49:17 +00:00
trace!("{entity:?}: Adding destination, zeroing velocity");
navigation_action.press(NavigationAction::SetLinearVelocity);
navigation_action
.action_data_mut(NavigationAction::SetLinearVelocity)
.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
commands.entity(entity).insert(dest);
}
} else if destination.is_some() {
2023-03-28 13:49:17 +00:00
commands
.entity(entity)
.remove::<Destination>()
.remove::<NoPath>();
}
2023-03-28 13:49:17 +00:00
actions.release(NegotiatePathAction);
actions.action_data_mut(NegotiatePathAction).axis_pair = None;
2021-05-13 17:25:45 +00:00
}
}
2021-06-15 22:48:49 +00:00
}
2021-05-13 17:25:45 +00:00
pub struct PathfindingPlugin;
2022-03-15 15:37:28 +00:00
impl Plugin for PathfindingPlugin {
2022-01-10 19:50:52 +00:00
fn build(&self, app: &mut App) {
app.add_plugin(InputManagerPlugin::<NegotiatePathAction>::default())
.register_type::<Destination>()
2022-12-19 20:08:31 +00:00
.register_type::<NoPath>()
.register_type::<Path>()
.register_type::<CostMap>()
2023-03-28 16:57:37 +00:00
.add_systems((negotiate_path, poll_tasks).in_base_set(CoreSet::PreUpdate))
.add_systems(
(actions, calculate_path)
.chain()
.in_base_set(CoreSet::PreUpdate)
.after(InputManagerSystem::Tick),
)
2023-03-28 16:57:37 +00:00
.add_system(remove_destination.in_base_set(CoreSet::PostUpdate));
2021-05-13 17:25:45 +00:00
}
}