mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-24 23:15:24 +03:00
change intersection policy from trait to enum, so serde works
This commit is contained in:
parent
ff2f284d1f
commit
e292d06cfb
1
TODO.md
1
TODO.md
@ -4,6 +4,7 @@
|
||||
|
||||
- cars seemingly whiz past the queue at stop signs (possibly also signals)
|
||||
- at signals, cars doing the same turn wont start it until the last car finishes it
|
||||
- draw cars in intersections, even when slightly zoomed out
|
||||
- draw cars in slightly different colors, to distinguish them better
|
||||
|
||||
- traffic signals
|
||||
|
@ -28,19 +28,41 @@ const WAIT_AT_STOP_SIGN: si::Second<f64> = si::Second {
|
||||
_marker: std::marker::PhantomData,
|
||||
};
|
||||
|
||||
pub trait IntersectionPolicy {
|
||||
// Use an enum instead of traits so that serialization works. I couldn't figure out erased_serde.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum IntersectionPolicy {
|
||||
StopSignPolicy(StopSign),
|
||||
TrafficSignalPolicy(TrafficSignal),
|
||||
}
|
||||
|
||||
impl IntersectionPolicy {
|
||||
// This must only be called when the car is ready to enter the intersection.
|
||||
fn can_do_turn(
|
||||
pub fn can_do_turn(
|
||||
&mut self,
|
||||
car: CarID,
|
||||
turn: TurnID,
|
||||
time: si::Second<f64>,
|
||||
geom_map: &GeomMap,
|
||||
control_map: &ControlMap,
|
||||
) -> bool;
|
||||
) -> bool {
|
||||
match *self {
|
||||
IntersectionPolicy::StopSignPolicy(ref mut p) => p.can_do_turn(car, turn, time, geom_map, control_map),
|
||||
IntersectionPolicy::TrafficSignalPolicy(ref mut p) => p.can_do_turn(car, turn, time, geom_map, control_map),
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&self, car: CarID);
|
||||
fn on_exit(&mut self, car: CarID);
|
||||
pub fn on_enter(&self, car: CarID) {
|
||||
match *self {
|
||||
IntersectionPolicy::StopSignPolicy(ref p) => p.on_enter(car),
|
||||
IntersectionPolicy::TrafficSignalPolicy(ref p) => p.on_enter(car),
|
||||
}
|
||||
}
|
||||
pub fn on_exit(&mut self, car: CarID) {
|
||||
match *self {
|
||||
IntersectionPolicy::StopSignPolicy(ref mut p) => p.on_exit(car),
|
||||
IntersectionPolicy::TrafficSignalPolicy(ref mut p) => p.on_exit(car),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@ -86,9 +108,7 @@ impl StopSign {
|
||||
})
|
||||
.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntersectionPolicy for StopSign {
|
||||
fn can_do_turn(
|
||||
&mut self,
|
||||
car: CarID,
|
||||
@ -153,9 +173,7 @@ impl TrafficSignal {
|
||||
accepted: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntersectionPolicy for TrafficSignal {
|
||||
// TODO determine if cars are staying in the intersection past the cycle time.
|
||||
|
||||
fn can_do_turn(
|
||||
|
@ -290,8 +290,7 @@ pub struct Sim {
|
||||
cars: HashMap<CarID, Car>,
|
||||
roads: Vec<SimQueue>,
|
||||
turns: Vec<SimQueue>,
|
||||
// TODO figure this out
|
||||
#[serde(skip_serializing, skip_deserializing)] intersections: Vec<Box<IntersectionPolicy>>,
|
||||
intersections: Vec<IntersectionPolicy>,
|
||||
#[serde(serialize_with = "serialize_s", deserialize_with = "deserialize_s")]
|
||||
pub time: si::Second<f64>,
|
||||
id_counter: usize,
|
||||
@ -300,12 +299,12 @@ pub struct Sim {
|
||||
|
||||
impl Sim {
|
||||
pub fn new(map: &Map, geom_map: &GeomMap) -> Sim {
|
||||
let mut intersections: Vec<Box<IntersectionPolicy>> = Vec::new();
|
||||
let mut intersections: Vec<IntersectionPolicy> = Vec::new();
|
||||
for i in map.all_intersections() {
|
||||
if i.has_traffic_signal {
|
||||
intersections.push(Box::new(TrafficSignal::new(i.id)));
|
||||
intersections.push(IntersectionPolicy::TrafficSignalPolicy(TrafficSignal::new(i.id)));
|
||||
} else {
|
||||
intersections.push(Box::new(StopSign::new(i.id)));
|
||||
intersections.push(IntersectionPolicy::StopSignPolicy(StopSign::new(i.id)));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user