Skip to main content

elevator_core/dispatch/
nearest_car.rs

1//! Nearest-car dispatch — assigns each call to the closest idle elevator.
2
3use crate::entity::EntityId;
4use crate::world::World;
5
6use super::{DispatchManifest, DispatchStrategy, ElevatorGroup};
7
8/// Scores `(car, stop)` by absolute distance between the car and the stop.
9///
10/// Paired with the Hungarian assignment in the dispatch system, this
11/// yields the globally minimum-total-distance matching across the group
12/// — no two cars can be sent to the same hall call.
13pub struct NearestCarDispatch;
14
15impl NearestCarDispatch {
16    /// Create a new `NearestCarDispatch`.
17    #[must_use]
18    pub const fn new() -> Self {
19        Self
20    }
21}
22
23impl Default for NearestCarDispatch {
24    fn default() -> Self {
25        Self::new()
26    }
27}
28
29impl DispatchStrategy for NearestCarDispatch {
30    fn rank(
31        &mut self,
32        _car: EntityId,
33        car_position: f64,
34        _stop: EntityId,
35        stop_position: f64,
36        _group: &ElevatorGroup,
37        _manifest: &DispatchManifest,
38        _world: &World,
39    ) -> Option<f64> {
40        Some((car_position - stop_position).abs())
41    }
42}