Skip to main content

elevator_core/components/
hall_call.rs

1//! Hall calls: the "up"/"down" buttons at each stop.
2//!
3//! A [`HallCall`] is the sim's representation of a pressed hall button.
4//! At most two calls exist per stop (one per [`CallDirection`]), aggregated
5//! across every rider who wants to go that direction. Calls are the unit
6//! dispatch strategies see — not riders — so the sim can model real
7//! collective-control elevators where a car doesn't know *who* is waiting,
8//! only that someone going up has pressed the button on floor N.
9//!
10//! ## Lifecycle
11//!
12//! 1. **Pressed** — a rider spawns or a game explicitly calls
13//!    [`Simulation::press_hall_button`](crate::sim::Simulation::press_hall_button).
14//!    `HallCall::press_tick` is set; `acknowledged_at` is `None`.
15//! 2. **Acknowledged** — after the group's `ack_latency_ticks` have elapsed,
16//!    `acknowledged_at` is set and the call becomes visible to dispatch.
17//! 3. **Assigned** — dispatch pairs the call with a car. The assignment
18//!    is keyed per-line in [`HallCall::assigned_cars_by_line`] so a stop
19//!    shared by multiple lines (e.g. a sky-lobby served by low, high, and
20//!    express banks) can record every bank's choice independently. Within
21//!    a single line the latest assignment replaces the previous one; across
22//!    lines the map grows until the call is cleared or the car is removed.
23//! 4. **Cleared** — the assigned car arrives at this stop with its
24//!    indicator lamps matching `direction` and opens doors. The HallCall
25//!    is removed; an `Event::HallCallCleared` is emitted.
26
27use std::collections::BTreeMap;
28
29use serde::{Deserialize, Serialize};
30
31use crate::entity::EntityId;
32
33/// Direction a hall call is requesting service in.
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
35#[non_exhaustive]
36pub enum CallDirection {
37    /// Requesting service upward (toward higher position).
38    Up,
39    /// Requesting service downward (toward lower position).
40    Down,
41}
42
43impl CallDirection {
44    /// Derive a call direction from the sign of `dest_pos - origin_pos`.
45    /// Returns `None` when the two stops share a position (no travel
46    /// needed — no hall call required).
47    #[must_use]
48    pub fn between(origin_pos: f64, dest_pos: f64) -> Option<Self> {
49        if dest_pos > origin_pos {
50            Some(Self::Up)
51        } else if dest_pos < origin_pos {
52            Some(Self::Down)
53        } else {
54            None
55        }
56    }
57
58    /// The opposite direction.
59    #[must_use]
60    pub const fn opposite(self) -> Self {
61        match self {
62            Self::Up => Self::Down,
63            Self::Down => Self::Up,
64        }
65    }
66}
67
68impl std::fmt::Display for CallDirection {
69    /// ```
70    /// # use elevator_core::components::CallDirection;
71    /// assert_eq!(format!("{}", CallDirection::Up), "up");
72    /// assert_eq!(format!("{}", CallDirection::Down), "down");
73    /// ```
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        match self {
76            Self::Up => f.write_str("up"),
77            Self::Down => f.write_str("down"),
78        }
79    }
80}
81
82/// A pressed hall button at `stop` requesting service in `direction`.
83///
84/// Stored per `(stop, direction)` pair — at most two per stop. Built-in
85/// dispatch reads calls via [`DispatchManifest::iter_hall_calls`](
86/// crate::dispatch::DispatchManifest::iter_hall_calls).
87#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
88#[non_exhaustive]
89pub struct HallCall {
90    /// Stop where the button was pressed.
91    pub stop: EntityId,
92    /// Direction the button requests.
93    pub direction: CallDirection,
94    /// Tick at which the button was first pressed.
95    pub press_tick: u64,
96    /// Tick at which dispatch first sees this call (after ack latency).
97    /// `None` while still pending acknowledgement.
98    pub acknowledged_at: Option<u64>,
99    /// Ticks the controller took to acknowledge this call, copied from
100    /// the serving group's [`ElevatorGroup::ack_latency_ticks`](
101    /// crate::dispatch::ElevatorGroup::ack_latency_ticks) when the
102    /// button was first pressed. Stored on the call itself so
103    /// `advance_transient` can tick the counter without needing to
104    /// look up the group.
105    pub ack_latency_ticks: u32,
106    /// Riders currently waiting on this call. Empty in
107    /// [`HallCallMode::Destination`](crate::dispatch::HallCallMode) mode
108    /// — calls there carry a single destination per press instead of a
109    /// shared direction.
110    pub pending_riders: Vec<EntityId>,
111    /// Destination requested at press time. Populated in
112    /// [`HallCallMode::Destination`](crate::dispatch::HallCallMode) mode
113    /// (lobby kiosk); `None` in Classic mode.
114    pub destination: Option<EntityId>,
115    /// Cars assigned to this call by dispatch, keyed by the line entity
116    /// the car runs on. A stop served by multiple lines can hold one
117    /// entry per line simultaneously — the low-bank car, the express
118    /// car, and the service car can all be en route to the same lobby
119    /// without one overwriting another. Within a single line the latest
120    /// assignment replaces the previous one.
121    ///
122    /// Pre-15.23 snapshots stored a single `assigned_car: Option<EntityId>`
123    /// field. Those snapshots silently drop the transient assignment on
124    /// load (serde's default unknown-field handling); the next dispatch
125    /// pass repopulates this map.
126    #[serde(default)]
127    pub assigned_cars_by_line: BTreeMap<EntityId, EntityId>,
128    /// When `true`, dispatch is forbidden from reassigning this call to
129    /// a different car. Set by
130    /// [`Simulation::pin_assignment`](crate::sim::Simulation::pin_assignment).
131    pub pinned: bool,
132}
133
134impl HallCall {
135    /// Create a new unacknowledged, unassigned hall call.
136    #[must_use]
137    pub const fn new(stop: EntityId, direction: CallDirection, press_tick: u64) -> Self {
138        Self {
139            stop,
140            direction,
141            press_tick,
142            acknowledged_at: None,
143            ack_latency_ticks: 0,
144            pending_riders: Vec::new(),
145            destination: None,
146            assigned_cars_by_line: BTreeMap::new(),
147            pinned: false,
148        }
149    }
150
151    /// Returns `true` when dispatch is allowed to see this call (ack
152    /// latency has elapsed).
153    #[must_use]
154    pub const fn is_acknowledged(&self) -> bool {
155        self.acknowledged_at.is_some()
156    }
157
158    /// Any car currently assigned to this call, preferring the entry
159    /// with the numerically smallest line-entity key (stable across
160    /// ticks because `BTreeMap` iteration is ordered). `None` when no
161    /// line has recorded an assignment yet.
162    ///
163    /// This matches the pre-per-line `assigned_car` semantics for
164    /// callers that just want "is anyone coming?" The richer shape is
165    /// available directly on [`assigned_cars_by_line`](Self::assigned_cars_by_line).
166    #[must_use]
167    pub fn any_assigned_car(&self) -> Option<EntityId> {
168        self.assigned_cars_by_line.values().next().copied()
169    }
170}