elevator_core/dispatch/
look.rs1use std::collections::HashMap;
14
15use crate::entity::EntityId;
16use crate::world::World;
17
18use super::sweep::{self, SweepDirection, SweepMode};
19use super::{DispatchManifest, DispatchStrategy, ElevatorGroup};
20
21pub struct LookDispatch {
23 direction: HashMap<EntityId, SweepDirection>,
25 mode: HashMap<EntityId, SweepMode>,
27}
28
29impl LookDispatch {
30 #[must_use]
32 pub fn new() -> Self {
33 Self {
34 direction: HashMap::new(),
35 mode: HashMap::new(),
36 }
37 }
38
39 fn direction_for(&self, car: EntityId) -> SweepDirection {
41 self.direction
42 .get(&car)
43 .copied()
44 .unwrap_or(SweepDirection::Up)
45 }
46
47 fn mode_for(&self, car: EntityId) -> SweepMode {
49 self.mode.get(&car).copied().unwrap_or(SweepMode::Strict)
50 }
51}
52
53impl Default for LookDispatch {
54 fn default() -> Self {
55 Self::new()
56 }
57}
58
59impl DispatchStrategy for LookDispatch {
60 fn prepare_car(
61 &mut self,
62 car: EntityId,
63 car_position: f64,
64 group: &ElevatorGroup,
65 manifest: &DispatchManifest,
66 world: &World,
67 ) {
68 let current = self.direction_for(car);
69 if sweep::strict_demand_ahead(current, car_position, group, manifest, world) {
70 self.mode.insert(car, SweepMode::Strict);
71 } else {
72 self.direction.insert(car, current.reversed());
73 self.mode.insert(car, SweepMode::Lenient);
74 }
75 }
76
77 fn rank(
78 &mut self,
79 car: EntityId,
80 car_position: f64,
81 _stop: EntityId,
82 stop_position: f64,
83 _group: &ElevatorGroup,
84 _manifest: &DispatchManifest,
85 _world: &World,
86 ) -> Option<f64> {
87 sweep::rank(
88 self.mode_for(car),
89 self.direction_for(car),
90 car_position,
91 stop_position,
92 )
93 }
94
95 fn notify_removed(&mut self, elevator: EntityId) {
96 self.direction.remove(&elevator);
97 self.mode.remove(&elevator);
98 }
99}