1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use abstutil::{deserialize_btreemap, serialize_btreemap};
use geom::Time;
use map_model::{BusRoute, BusRouteID, BusStopID, Map, Path, PathRequest, Position};

use crate::sim::Ctx;
use crate::{
    AgentID, CarID, DrivingSimState, Event, PedestrianID, PersonID, Router, TripID, TripManager,
    TripPhaseType, UnzoomedAgent, VehicleType, WalkingSimState,
};

// These index stops along a route, not stops along a single sidewalk.
type StopIdx = usize;

#[derive(Serialize, Deserialize, Clone)]
struct Stop {
    id: BusStopID,
    driving_pos: Position,
    next_stop: Option<Path>,
}

#[derive(Serialize, Deserialize, Clone)]
struct Route {
    stops: Vec<Stop>,
    start: Path,
    end_at_border: Option<Path>,
    active_vehicles: BTreeSet<CarID>,
}

#[derive(Serialize, Deserialize, Clone)]
struct Bus {
    car: CarID,
    route: BusRouteID,
    /// Where does each passenger want to deboard?
    passengers: Vec<(PersonID, Option<BusStopID>)>,
    state: BusState,
}

#[derive(Serialize, Deserialize, Clone)]
enum BusState {
    DrivingToStop(StopIdx),
    AtStop(StopIdx),
    DrivingOffMap,
    Done,
}

/// Manages public transit vehicles (buses and trains) that follow a route. The transit model is
/// currently kind of broken, so not describing the state machine yet.
#[derive(Serialize, Deserialize, Clone)]
pub(crate) struct TransitSimState {
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    buses: BTreeMap<CarID, Bus>,
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    routes: BTreeMap<BusRouteID, Route>,
    /// waiting at => (ped, route, bound for, started waiting)
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    peds_waiting: BTreeMap<BusStopID, Vec<(PedestrianID, BusRouteID, Option<BusStopID>, Time)>>,

    events: Vec<Event>,
}

impl TransitSimState {
    pub fn new(map: &Map) -> TransitSimState {
        // Keep this filled out always so get_passengers can return &Vec without a hassle
        let mut peds_waiting = BTreeMap::new();
        for bs in map.all_bus_stops().keys() {
            peds_waiting.insert(*bs, Vec::new());
        }

        TransitSimState {
            buses: BTreeMap::new(),
            routes: BTreeMap::new(),
            peds_waiting,
            events: Vec::new(),
        }
    }

    /// Returns the path for the first leg.
    pub fn create_empty_route(&mut self, bus_route: &BusRoute, map: &Map) -> Path {
        self.routes.entry(bus_route.id).or_insert_with(|| {
            assert!(bus_route.stops.len() > 1);
            let mut stops = Vec::new();
            for (idx, stop1_id) in bus_route.stops.iter().enumerate() {
                let stop1 = map.get_bs(*stop1_id);
                if idx == bus_route.stops.len() - 1 {
                    stops.push(Stop {
                        id: stop1.id,
                        driving_pos: stop1.driving_pos,
                        next_stop: None,
                    });
                    continue;
                }
                let req = PathRequest::vehicle(
                    stop1.driving_pos,
                    map.get_bs(bus_route.stops[idx + 1]).driving_pos,
                    bus_route.route_type,
                );
                match map.pathfind(req) {
                    Ok(path) => {
                        if path.is_empty() {
                            panic!("Empty path between stops?! {}", path.get_req());
                        }
                        stops.push(Stop {
                            id: stop1.id,
                            driving_pos: stop1.driving_pos,
                            next_stop: Some(path),
                        });
                    }
                    Err(err) => {
                        panic!("No route between stops: {}", err);
                    }
                }
            }
            let start_req = PathRequest::vehicle(
                Position::start(bus_route.start),
                map.get_bs(bus_route.stops[0]).driving_pos,
                bus_route.route_type,
            );
            let start = map.pathfind(start_req).expect("no route to first stop");
            let end_at_border = if let Some(l) = bus_route.end_border {
                let req = PathRequest::vehicle(
                    map.get_bs(*bus_route.stops.last().unwrap()).driving_pos,
                    Position::end(l, map),
                    bus_route.route_type,
                );
                let path = map
                    .pathfind(req)
                    .expect("no route from last stop to border");
                Some(path)
            } else {
                None
            };
            Route {
                active_vehicles: BTreeSet::new(),
                stops,
                start,
                end_at_border,
            }
        });

        self.routes[&bus_route.id].start.clone()
    }

    pub fn bus_created(&mut self, bus: CarID, r: BusRouteID) {
        let route = self.routes.get_mut(&r).unwrap();
        route.active_vehicles.insert(bus);
        self.buses.insert(
            bus,
            Bus {
                car: bus,
                route: r,
                passengers: Vec::new(),
                state: BusState::DrivingToStop(0),
            },
        );
    }

    /// If true, the bus is idling. If false, the bus actually arrived at a border and should now
    /// vanish.
    pub fn bus_arrived_at_stop(
        &mut self,
        now: Time,
        id: CarID,
        trips: &mut TripManager,
        walking: &mut WalkingSimState,
        ctx: &mut Ctx,
    ) -> bool {
        let mut bus = self.buses.get_mut(&id).unwrap();
        match bus.state {
            BusState::DrivingToStop(stop_idx) => {
                bus.state = BusState::AtStop(stop_idx);
                let stop1 = self.routes[&bus.route].stops[stop_idx].id;
                self.events
                    .push(Event::BusArrivedAtStop(id, bus.route, stop1));

                // Deboard existing passengers.
                let mut still_riding = Vec::new();
                for (person, maybe_stop2) in bus.passengers.drain(..) {
                    if Some(stop1) == maybe_stop2 {
                        trips.person_left_bus(now, person, bus.car, ctx);
                        self.events.push(Event::PassengerAlightsTransit(
                            person, bus.car, bus.route, stop1,
                        ));
                    } else {
                        still_riding.push((person, maybe_stop2));
                    }
                }
                bus.passengers = still_riding;

                // Board new passengers.
                let mut still_waiting = Vec::new();
                for (ped, route, maybe_stop2, started_waiting) in
                    self.peds_waiting.remove(&stop1).unwrap()
                {
                    if bus.route == route {
                        let (trip, person) = trips.ped_boarded_bus(
                            now,
                            ped,
                            bus.car,
                            now - started_waiting,
                            walking,
                        );
                        self.events.push(Event::PassengerBoardsTransit(
                            person,
                            bus.car,
                            bus.route,
                            stop1,
                            now - started_waiting,
                        ));
                        self.events.push(Event::TripPhaseStarting(
                            trip,
                            person,
                            Some(PathRequest::vehicle(
                                ctx.map.get_bs(stop1).driving_pos,
                                if let Some(stop2) = maybe_stop2 {
                                    ctx.map.get_bs(stop2).driving_pos
                                } else {
                                    self.routes[&route]
                                        .end_at_border
                                        .as_ref()
                                        .unwrap()
                                        .get_req()
                                        .end
                                },
                                bus.car.vehicle_type.to_constraints(),
                            )),
                            TripPhaseType::RidingBus(route, stop1, bus.car),
                        ));
                        bus.passengers.push((person, maybe_stop2));
                    } else {
                        still_waiting.push((ped, route, maybe_stop2, started_waiting));
                    }
                }
                self.peds_waiting.insert(stop1, still_waiting);
                true
            }
            BusState::DrivingOffMap => {
                self.routes
                    .get_mut(&bus.route)
                    .unwrap()
                    .active_vehicles
                    .remove(&id);
                bus.state = BusState::Done;
                for (person, maybe_stop2) in bus.passengers.drain(..) {
                    if let Some(stop2) = maybe_stop2 {
                        panic!(
                            "{} fell asleep on {} and just rode off-map, but they were supposed \
                             to hop off at {}",
                            person, bus.car, stop2
                        );
                    }
                    trips.transit_rider_reached_border(now, person, id, ctx);
                }
                false
            }
            BusState::AtStop(_) | BusState::Done => unreachable!(),
        }
    }

    pub fn bus_departed_from_stop(&mut self, id: CarID, map: &Map) -> Router {
        let mut bus = self.buses.get_mut(&id).unwrap();
        let route = self.routes.get_mut(&bus.route).unwrap();
        match bus.state {
            BusState::DrivingToStop(_) | BusState::DrivingOffMap | BusState::Done => unreachable!(),
            BusState::AtStop(stop_idx) => {
                let stop = &route.stops[stop_idx];
                self.events
                    .push(Event::BusDepartedFromStop(id, bus.route, stop.id));
                if let Some(path) = stop.next_stop.clone() {
                    bus.state = BusState::DrivingToStop(stop_idx + 1);
                    Router::follow_bus_route(id, path)
                } else if let Some(path) = route.end_at_border.clone() {
                    bus.state = BusState::DrivingOffMap;
                    Router::follow_bus_route(id, path)
                } else {
                    route.active_vehicles.remove(&id);
                    for (person, stop2) in &bus.passengers {
                        panic!(
                            "{} of {} is vanishing at its last stop, but {} is still riding \
                             until {:?}",
                            bus.car, bus.route, person, stop2
                        );
                    }
                    bus.state = BusState::Done;
                    Router::vanish_bus(id, stop.driving_pos, map)
                }
            }
        }
    }

    /// Returns the bus if the pedestrian boarded immediately.
    pub fn ped_waiting_for_bus(
        &mut self,
        now: Time,
        ped: PedestrianID,
        trip: TripID,
        person: PersonID,
        stop1: BusStopID,
        route_id: BusRouteID,
        maybe_stop2: Option<BusStopID>,
        map: &Map,
    ) -> Option<CarID> {
        assert!(Some(stop1) != maybe_stop2);
        if let Some(route) = self.routes.get(&route_id) {
            for bus in &route.active_vehicles {
                if let BusState::AtStop(idx) = self.buses[bus].state {
                    if route.stops[idx].id == stop1 {
                        self.buses
                            .get_mut(bus)
                            .unwrap()
                            .passengers
                            .push((person, maybe_stop2));
                        self.events.push(Event::TripPhaseStarting(
                            trip,
                            person,
                            Some(PathRequest::vehicle(
                                map.get_bs(stop1).driving_pos,
                                if let Some(stop2) = maybe_stop2 {
                                    map.get_bs(stop2).driving_pos
                                } else {
                                    route.end_at_border.as_ref().unwrap().get_req().end
                                },
                                bus.vehicle_type.to_constraints(),
                            )),
                            TripPhaseType::RidingBus(route_id, stop1, *bus),
                        ));
                        return Some(*bus);
                    }
                }
            }
        } else {
            println!(
                "WARNING: {} waiting for {}, but that route hasn't been instantiated",
                ped, route_id
            );
        }

        self.peds_waiting
            .get_mut(&stop1)
            .unwrap()
            .push((ped, route_id, maybe_stop2, now));
        None
    }

    pub fn collect_events(&mut self) -> Vec<Event> {
        self.events.drain(..).collect()
    }

    pub fn get_passengers(&self, bus: CarID) -> &Vec<(PersonID, Option<BusStopID>)> {
        &self.buses[&bus].passengers
    }

    pub fn bus_route(&self, bus: CarID) -> BusRouteID {
        self.buses[&bus].route
    }

    /// also stop idx that the bus is coming from
    pub fn buses_for_route(&self, route: BusRouteID) -> Vec<(CarID, Option<usize>)> {
        if let Some(r) = self.routes.get(&route) {
            r.active_vehicles
                .iter()
                .map(|bus| {
                    let stop = match self.buses[bus].state {
                        BusState::DrivingToStop(idx) => {
                            if idx == 0 {
                                None
                            } else {
                                Some(idx - 1)
                            }
                        }
                        BusState::AtStop(idx) => Some(idx),
                        BusState::DrivingOffMap => Some(r.stops.len() - 1),
                        BusState::Done => unreachable!(),
                    };
                    (*bus, stop)
                })
                .collect()
        } else {
            Vec::new()
        }
    }

    /// (buses, trains)
    pub fn active_vehicles(&self) -> (usize, usize) {
        let mut buses = 0;
        let mut trains = 0;
        for r in self.routes.values() {
            let len = r.active_vehicles.len();
            if len > 0 {
                if r.active_vehicles.iter().next().unwrap().vehicle_type == VehicleType::Bus {
                    buses += len;
                } else {
                    trains += len;
                }
            }
        }
        (buses, trains)
    }

    pub fn get_people_waiting_at_stop(
        &self,
        at: BusStopID,
    ) -> &Vec<(PedestrianID, BusRouteID, Option<BusStopID>, Time)> {
        &self.peds_waiting[&at]
    }

    pub fn get_unzoomed_transit_riders(
        &self,
        now: Time,
        driving: &DrivingSimState,
        map: &Map,
    ) -> Vec<UnzoomedAgent> {
        let mut results = Vec::new();
        for (bus_id, bus) in &self.buses {
            if bus.passengers.is_empty() {
                continue;
            }
            let pos = if let Some(input) = driving.get_single_draw_car(*bus_id, now, map, self) {
                input.body.last_pt()
            } else {
                panic!(
                    "At {}, bus {} can't be drawn, yet it has passengers {:?}",
                    now, bus_id, bus.passengers
                );
            };
            for (person, _) in &bus.passengers {
                let agent = AgentID::BusPassenger(*person, *bus_id);
                results.push(UnzoomedAgent {
                    id: agent,
                    pos,
                    person: Some(*person),
                    parking: false,
                });
            }
        }
        results
    }
}