show route by trip, not agent

This commit is contained in:
Dustin Carlino 2018-10-22 14:34:00 -07:00
parent afa52e4bad
commit 9660794467
3 changed files with 42 additions and 39 deletions

View File

@ -2,61 +2,60 @@ use colors::Colors;
use dimensioned::si; use dimensioned::si;
use ezgui::GfxCtx; use ezgui::GfxCtx;
use map_model::{Trace, LANE_THICKNESS}; use map_model::{Trace, LANE_THICKNESS};
use objects::{Ctx, ID}; use objects::Ctx;
use piston::input::Key; use piston::input::Key;
use plugins::{Plugin, PluginCtx}; use plugins::{Plugin, PluginCtx};
use sim::AgentID; use sim::TripID;
use std::f64; use std::f64;
pub enum ShowRouteState { pub enum ShowRouteState {
Empty, Empty,
Active(AgentID, Trace), Active(TripID, Trace),
} }
impl Plugin for ShowRouteState { impl Plugin for ShowRouteState {
fn event(&mut self, ctx: PluginCtx) -> bool { fn event(&mut self, ctx: PluginCtx) -> bool {
let (input, sim, map, selected) = ( let maybe_trip = match self {
ctx.input, ShowRouteState::Empty => ctx
&ctx.primary.sim, .primary
&ctx.primary.map, .current_selection
ctx.primary.current_selection, .and_then(|id| id.agent_id())
); .and_then(|agent| ctx.primary.sim.agent_to_trip(agent))
.and_then(|trip| {
let maybe_agent = match self { if ctx
ShowRouteState::Empty => match selected { .input
Some(ID::Car(id)) => { .key_pressed(Key::R, &format!("show {}'s route", trip))
if input.key_pressed(Key::R, "show this car's route") { {
Some(AgentID::Car(id)) Some(trip)
} else { } else {
None None
} }
} }),
Some(ID::Pedestrian(id)) => { ShowRouteState::Active(trip, _) => {
if input.key_pressed(Key::R, "show this pedestrian's route") { if ctx.input.key_pressed(Key::Return, "stop showing route") {
Some(AgentID::Pedestrian(id))
} else {
None
}
}
_ => None,
},
ShowRouteState::Active(agent, _) => {
if input.key_pressed(Key::Return, "stop showing route") {
None None
} else { } else {
Some(*agent) Some(*trip)
} }
} }
}; };
if let Some(agent) = maybe_agent { if let Some(trip) = maybe_trip {
// Trace along the entire route by passing in max distance if let Some(agent) = ctx.primary.sim.trip_to_agent(trip) {
match sim.trace_route(agent, map, f64::MAX * si::M) { // Trace along the entire route by passing in max distance
Some(trace) => { if let Some(trace) =
*self = ShowRouteState::Active(agent, trace); ctx.primary
} .sim
None => { .trace_route(agent, &ctx.primary.map, f64::MAX * si::M)
*self = ShowRouteState::Empty; {
*self = ShowRouteState::Active(trip, trace);
} else {
warn!("{} has no trace right now", agent);
} }
} else {
warn!(
"{} has no agent associated right now; is the trip done?",
trip
);
} }
} else { } else {
*self = ShowRouteState::Empty; *self = ShowRouteState::Empty;

View File

@ -384,7 +384,7 @@ impl Sim {
// TODO dont toggle state in debug_car // TODO dont toggle state in debug_car
pub fn debug_trip(&mut self, id: TripID) { pub fn debug_trip(&mut self, id: TripID) {
match self.trips_state.current_mode(id) { match self.trips_state.trip_to_agent(id) {
Some(AgentID::Car(id)) => self.debug_car(id), Some(AgentID::Car(id)) => self.debug_car(id),
Some(AgentID::Pedestrian(id)) => self.debug_ped(id), Some(AgentID::Pedestrian(id)) => self.debug_ped(id),
None => println!("{} doesn't exist", id), None => println!("{} doesn't exist", id),
@ -395,9 +395,13 @@ impl Sim {
self.trips_state.agent_to_trip(id) self.trips_state.agent_to_trip(id)
} }
pub fn trip_to_agent(&self, id: TripID) -> Option<AgentID> {
self.trips_state.trip_to_agent(id)
}
pub fn get_canonical_point_for_trip(&self, id: TripID, map: &Map) -> Option<Pt2D> { pub fn get_canonical_point_for_trip(&self, id: TripID, map: &Map) -> Option<Pt2D> {
// Don't unwrap(); the trip might be registered before the agent has started. // Don't unwrap(); the trip might be registered before the agent has started.
match self.trips_state.current_mode(id) { match self.trips_state.trip_to_agent(id) {
Some(AgentID::Car(id)) => self Some(AgentID::Car(id)) => self
.driving_state .driving_state
.get_draw_car(id, self.time, map) .get_draw_car(id, self.time, map)

View File

@ -226,7 +226,7 @@ impl TripManager {
self.active_trip_mode.keys().cloned().collect() self.active_trip_mode.keys().cloned().collect()
} }
pub fn current_mode(&self, id: TripID) -> Option<AgentID> { pub fn trip_to_agent(&self, id: TripID) -> Option<AgentID> {
let trip = self.trips.get(id.0)?; let trip = self.trips.get(id.0)?;
match trip.legs[0] { match trip.legs[0] {
TripLeg::Walk(_) => Some(AgentID::Pedestrian(trip.ped)), TripLeg::Walk(_) => Some(AgentID::Pedestrian(trip.ped)),