elevator_core/dispatch/
scan.rs1use std::collections::HashMap;
9
10use crate::entity::EntityId;
11use crate::world::World;
12
13use super::sweep::{self, SweepDirection, SweepMode};
14use super::{DispatchManifest, DispatchStrategy, ElevatorGroup, RankContext};
15
16pub struct ScanDispatch {
25 direction: HashMap<EntityId, SweepDirection>,
27 mode: HashMap<EntityId, SweepMode>,
29}
30
31impl ScanDispatch {
32 #[must_use]
34 pub fn new() -> Self {
35 Self {
36 direction: HashMap::new(),
37 mode: HashMap::new(),
38 }
39 }
40
41 fn direction_for(&self, car: EntityId) -> SweepDirection {
43 self.direction
44 .get(&car)
45 .copied()
46 .unwrap_or(SweepDirection::Up)
47 }
48
49 fn mode_for(&self, car: EntityId) -> SweepMode {
51 self.mode.get(&car).copied().unwrap_or(SweepMode::Strict)
52 }
53}
54
55impl Default for ScanDispatch {
56 fn default() -> Self {
57 Self::new()
58 }
59}
60
61impl DispatchStrategy for ScanDispatch {
62 fn prepare_car(
63 &mut self,
64 car: EntityId,
65 car_position: f64,
66 group: &ElevatorGroup,
67 manifest: &DispatchManifest,
68 world: &World,
69 ) {
70 let current = self.direction_for(car);
71 if sweep::strict_demand_ahead(current, car_position, group, manifest, world) {
72 self.mode.insert(car, SweepMode::Strict);
73 } else {
74 self.direction.insert(car, current.reversed());
75 self.mode.insert(car, SweepMode::Lenient);
76 }
77 }
78
79 fn rank(&mut self, ctx: &RankContext<'_>) -> Option<f64> {
80 sweep::rank(
81 self.mode_for(ctx.car),
82 self.direction_for(ctx.car),
83 ctx.car_position,
84 ctx.stop_position,
85 )
86 }
87
88 fn notify_removed(&mut self, elevator: EntityId) {
89 self.direction.remove(&elevator);
90 self.mode.remove(&elevator);
91 }
92}