2019-02-02 01:44:21 +03:00
|
|
|
use crate::{CarID, PedestrianID, Sim, Tick, VehicleType};
|
2019-01-11 02:11:16 +03:00
|
|
|
use geom::{PolyLine, Pt2D};
|
2018-11-27 02:45:54 +03:00
|
|
|
use map_model::{LaneType, Map, Trace, Traversable, TurnID};
|
2018-11-13 22:34:17 +03:00
|
|
|
|
|
|
|
// Intermediate structures so that sim and editor crates don't have a cyclic dependency.
|
2018-11-23 00:02:04 +03:00
|
|
|
#[derive(Clone)]
|
2018-11-13 22:34:17 +03:00
|
|
|
pub struct DrawPedestrianInput {
|
|
|
|
pub id: PedestrianID,
|
|
|
|
pub pos: Pt2D,
|
|
|
|
pub waiting_for_turn: Option<TurnID>,
|
2018-11-17 01:23:33 +03:00
|
|
|
pub preparing_bike: bool,
|
2018-12-03 22:12:05 +03:00
|
|
|
pub on: Traversable,
|
2018-11-13 22:34:17 +03:00
|
|
|
}
|
|
|
|
|
2018-11-23 00:02:04 +03:00
|
|
|
#[derive(Clone)]
|
2018-11-13 22:34:17 +03:00
|
|
|
pub struct DrawCarInput {
|
|
|
|
pub id: CarID,
|
|
|
|
pub waiting_for_turn: Option<TurnID>,
|
|
|
|
pub stopping_trace: Option<Trace>,
|
|
|
|
pub state: CarState,
|
2018-11-18 04:31:12 +03:00
|
|
|
pub vehicle_type: VehicleType,
|
2018-12-03 22:12:05 +03:00
|
|
|
pub on: Traversable,
|
2019-01-11 02:11:16 +03:00
|
|
|
|
|
|
|
// Starts at the BACK of the car. TODO Dedupe unused old stuff.
|
|
|
|
pub body: PolyLine,
|
2018-11-13 22:34:17 +03:00
|
|
|
}
|
|
|
|
|
2018-11-23 00:02:04 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2018-11-13 22:34:17 +03:00
|
|
|
pub enum CarState {
|
|
|
|
Moving,
|
|
|
|
Stuck,
|
|
|
|
Parked,
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
|
2018-11-23 00:02:04 +03:00
|
|
|
// TODO Can we return borrows instead? Nice for time travel, not for main sim?
|
|
|
|
// actually good for main sim too; we're constantly calculating stuff while sim is paused
|
|
|
|
// otherwise? except we don't know what to calculate. maybe cache it?
|
|
|
|
pub trait GetDrawAgents {
|
2019-02-02 01:44:21 +03:00
|
|
|
fn tick(&self) -> Tick;
|
2018-11-23 00:02:04 +03:00
|
|
|
fn get_draw_car(&self, id: CarID, map: &Map) -> Option<DrawCarInput>;
|
|
|
|
fn get_draw_ped(&self, id: PedestrianID, map: &Map) -> Option<DrawPedestrianInput>;
|
2018-11-27 02:45:54 +03:00
|
|
|
fn get_draw_cars(&self, on: Traversable, map: &Map) -> Vec<DrawCarInput>;
|
|
|
|
fn get_draw_peds(&self, on: Traversable, map: &Map) -> Vec<DrawPedestrianInput>;
|
2018-12-03 22:12:05 +03:00
|
|
|
fn get_all_draw_cars(&self, map: &Map) -> Vec<DrawCarInput>;
|
|
|
|
fn get_all_draw_peds(&self, map: &Map) -> Vec<DrawPedestrianInput>;
|
2018-11-23 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GetDrawAgents for Sim {
|
2019-02-02 01:44:21 +03:00
|
|
|
fn tick(&self) -> Tick {
|
|
|
|
self.time
|
|
|
|
}
|
|
|
|
|
2018-11-23 00:02:04 +03:00
|
|
|
fn get_draw_car(&self, id: CarID, map: &Map) -> Option<DrawCarInput> {
|
2018-11-13 22:34:17 +03:00
|
|
|
self.driving_state
|
|
|
|
.get_draw_car(id, self.time, map)
|
2019-01-11 02:11:16 +03:00
|
|
|
.or_else(|| self.parking_state.get_draw_car(id, map))
|
2018-11-13 22:34:17 +03:00
|
|
|
}
|
|
|
|
|
2018-11-23 00:02:04 +03:00
|
|
|
fn get_draw_ped(&self, id: PedestrianID, map: &Map) -> Option<DrawPedestrianInput> {
|
2018-11-19 03:50:21 +03:00
|
|
|
self.walking_state.get_draw_ped(id, map, self.time)
|
2018-11-13 22:34:17 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:45:54 +03:00
|
|
|
fn get_draw_cars(&self, on: Traversable, map: &Map) -> Vec<DrawCarInput> {
|
|
|
|
match on {
|
|
|
|
Traversable::Lane(l) => match map.get_l(l).lane_type {
|
|
|
|
LaneType::Driving | LaneType::Bus | LaneType::Biking => {
|
|
|
|
self.driving_state.get_draw_cars(on, self.time, map)
|
|
|
|
}
|
2019-01-11 02:11:16 +03:00
|
|
|
LaneType::Parking => self.parking_state.get_draw_cars(l, map),
|
2018-11-27 02:45:54 +03:00
|
|
|
LaneType::Sidewalk => Vec::new(),
|
|
|
|
},
|
|
|
|
Traversable::Turn(_) => self.driving_state.get_draw_cars(on, self.time, map),
|
2018-11-13 22:34:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:45:54 +03:00
|
|
|
fn get_draw_peds(&self, on: Traversable, map: &Map) -> Vec<DrawPedestrianInput> {
|
|
|
|
self.walking_state.get_draw_peds(on, map, self.time)
|
2018-11-13 22:34:17 +03:00
|
|
|
}
|
2018-12-03 22:12:05 +03:00
|
|
|
|
|
|
|
fn get_all_draw_cars(&self, map: &Map) -> Vec<DrawCarInput> {
|
|
|
|
let mut cars = self.driving_state.get_all_draw_cars(self.time, map);
|
2019-01-11 02:11:16 +03:00
|
|
|
cars.extend(self.parking_state.get_all_draw_cars(map));
|
2018-12-03 22:12:05 +03:00
|
|
|
cars
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_all_draw_peds(&self, map: &Map) -> Vec<DrawPedestrianInput> {
|
|
|
|
self.walking_state.get_all_draw_peds(self.time, map)
|
|
|
|
}
|
2018-11-13 22:34:17 +03:00
|
|
|
}
|