Skip to main content

elevator_core/
lib.rs

1//! # elevator-core
2//!
3//! Engine-agnostic, tick-based elevator simulation library for Rust.
4//!
5//! This crate provides the building blocks for modeling vertical transportation
6//! systems — from a 3-story office building to an orbital space elevator.
7//! Stops sit at arbitrary positions rather than uniform floors, and the
8//! simulation is driven by a deterministic 8-phase tick loop.
9//!
10//! ## Key capabilities
11//!
12//! - **Pluggable dispatch** — five built-in algorithms ([`dispatch::scan::ScanDispatch`],
13//!   [`dispatch::look::LookDispatch`], [`dispatch::nearest_car::NearestCarDispatch`],
14//!   [`dispatch::etd::EtdDispatch`], [`dispatch::destination::DestinationDispatch`])
15//!   plus the [`dispatch::DispatchStrategy`] trait for custom implementations.
16//! - **Trapezoidal motion profiles** — realistic acceleration, cruise, and
17//!   deceleration computed per-tick.
18//! - **Extension components** — attach arbitrary `Serialize + DeserializeOwned`
19//!   data to any entity via [`world::World::insert_ext`] without modifying the
20//!   library.
21//! - **Lifecycle hooks** — inject logic before or after any of the eight
22//!   simulation phases. See [`hooks::Phase`].
23//! - **Metrics and events** — query aggregate wait/ride times through
24//!   [`metrics::Metrics`] and react to fine-grained tick events via
25//!   [`events::Event`].
26//! - **Snapshot save/load** — capture and restore full simulation state with
27//!   [`snapshot::WorldSnapshot`].
28//! - **Zero `unsafe` code** — enforced by `#![forbid(unsafe_code)]`.
29//!
30//! ## Quick start
31//!
32//! ```rust
33//! use elevator_core::prelude::*;
34//! use elevator_core::stop::StopConfig;
35//!
36//! let mut sim = SimulationBuilder::demo()
37//!     .stops(vec![
38//!         StopConfig { id: StopId(0), name: "Ground".into(), position: 0.0 },
39//!         StopConfig { id: StopId(1), name: "Floor 2".into(), position: 4.0 },
40//!         StopConfig { id: StopId(2), name: "Floor 3".into(), position: 8.0 },
41//!     ])
42//!     .build()
43//!     .unwrap();
44//!
45//! sim.spawn_rider(StopId(0), StopId(2), 75.0).unwrap();
46//!
47//! for _ in 0..1000 {
48//!     sim.step();
49//! }
50//!
51//! assert!(sim.metrics().total_delivered() > 0);
52//! ```
53//!
54//! ## Crate layout
55//!
56//! ## Stability
57//!
58//! Every module below is classified as **stable**, **experimental**, or
59//! **internal**. Stable items break only in planned major versions
60//! with a deprecation cycle; experimental items may change in any
61//! minor version; internal items are not part of the supported surface.
62//! See [`STABILITY.md`](https://github.com/andymai/elevator-core/blob/main/STABILITY.md)
63//! for the full policy and cadence commitment.
64//!
65//! | Module | Purpose | Stability |
66//! |--------|---------|-----------|
67//! | [`builder`] | Fluent [`SimulationBuilder`](builder::SimulationBuilder) API | stable |
68//! | [`sim`] | Top-level [`Simulation`](sim::Simulation) runner and tick loop | stable (selected methods — see [`STABILITY.md`](https://github.com/andymai/elevator-core/blob/main/STABILITY.md)) |
69//! | [`dispatch`] | Dispatch strategies and the [`DispatchStrategy`](dispatch::DispatchStrategy) trait | stable |
70//! | [`world`] | ECS-style [`World`](world::World) with typed component storage | experimental |
71//! | [`components`] | Entity data types: [`Rider`](components::Rider), [`Elevator`](components::Elevator), [`Stop`](components::Stop), [`Line`](components::Line), [`Route`](components::Route), [`Patience`](components::Patience), [`Preferences`](components::Preferences), [`AccessControl`](components::AccessControl), [`DestinationQueue`](components::DestinationQueue), [`ServiceMode`](components::ServiceMode), [`Orientation`](components::Orientation), [`Position`](components::Position), [`Velocity`](components::Velocity), [`SpatialPosition`](components::SpatialPosition) | stable (units + core), experimental (rest) |
72//! | [`config`] | RON-deserializable [`SimConfig`](config::SimConfig), [`GroupConfig`](config::GroupConfig), [`LineConfig`](config::LineConfig) | experimental |
73//! | [`events`] | [`Event`](events::Event) variants and the [`EventBus`](events::EventBus) | stable ([`Event`](events::Event)) |
74//! | [`metrics`] | Aggregate [`Metrics`](metrics::Metrics) (wait time, throughput, etc.) | stable |
75//! | [`hooks`] | Lifecycle hook registration by [`Phase`](hooks::Phase) | experimental |
76//! | [`query`] | Entity query builder for filtering by component composition | experimental |
77//! | [`systems`] | Per-phase tick logic (dispatch, movement, doors, loading, ...) | internal |
78//! | [`snapshot`] | [`WorldSnapshot`](snapshot::WorldSnapshot) save/restore with custom-strategy factory | stable |
79//! | [`scenario`] | Deterministic scenario replay from recorded event streams | experimental |
80//! | [`topology`] | Lazy-rebuilt connectivity graph for cross-line routing | experimental |
81//! | [`traffic`] | [`TrafficSource`](traffic::TrafficSource) trait + `PoissonSource` (feature-gated) | experimental |
82//! | [`tagged_metrics`] | Per-tag metric accumulators for zone/line/priority breakdowns | experimental |
83//! | [`movement`] | Trapezoidal velocity-profile primitives ([`braking_distance`](movement::braking_distance), [`tick_movement`](movement::tick_movement)) | experimental |
84//! | [`door`] | Door finite-state machine ([`DoorState`](door::DoorState)) | internal |
85//! | [`eta`] | Per-stop arrival-time estimation used by dispatch | internal |
86//! | [`time`] | Tick-to-wall-clock conversion ([`TimeAdapter`](time::TimeAdapter)) | experimental |
87//! | `energy` | Simplified per-elevator energy modeling (gated behind the `energy` feature) | experimental |
88//! | [`stop`] | [`StopId`](stop::StopId) and [`StopConfig`](stop::StopConfig) | stable |
89//! | [`entity`] | Opaque [`EntityId`](entity::EntityId) runtime identity | stable |
90//! | [`ids`] | Config-level typed identifiers ([`GroupId`](ids::GroupId), etc.) | experimental |
91//! | [`error`] | [`SimError`](error::SimError), [`RejectionReason`](error::RejectionReason), [`RejectionContext`](error::RejectionContext) | stable ([`SimError`](error::SimError)) |
92//!
93//! ## Architecture overview
94//!
95//! ### 8-phase tick loop
96//!
97//! Each call to [`Simulation::step()`](sim::Simulation::step) runs these
98//! phases in order:
99//!
100//! 1. **`AdvanceTransient`** — transitions `Boarding→Riding`, `Exiting→Arrived`,
101//!    teleports walkers.
102//! 2. **Dispatch** — builds a [`DispatchManifest`](dispatch::DispatchManifest)
103//!    and calls each group's [`DispatchStrategy`](dispatch::DispatchStrategy).
104//! 3. **Reposition** — optional phase; moves idle elevators via
105//!    [`RepositionStrategy`](dispatch::RepositionStrategy) for better coverage.
106//! 4. **`AdvanceQueue`** — reconciles each elevator's phase/target with the
107//!    front of its [`DestinationQueue`](components::DestinationQueue), so
108//!    imperative pushes from game code take effect before movement.
109//! 5. **Movement** — applies trapezoidal velocity profiles, detects stop arrivals
110//!    and emits [`PassingFloor`](events::Event::PassingFloor) events.
111//! 6. **Doors** — ticks the [`DoorState`](door::DoorState) FSM per elevator.
112//! 7. **Loading** — boards/exits riders with capacity and preference checks.
113//! 8. **Metrics** — aggregates wait/ride times into [`Metrics`](metrics::Metrics)
114//!    and per-tag accumulators.
115//!
116//! For full per-phase semantics (events emitted, edge cases, design rationale),
117//! see the [guide](https://andymai.github.io/elevator-core/simulation-loop.html).
118//!
119//! ### Component relationships
120//!
121//! ```text
122//! Group ──contains──▶ Line ──has──▶ Elevator ──carries──▶ Rider
123//!   │                  │              │                      │
124//!   └── DispatchStrategy              └── Position           └── Route (optional)
125//!        RepositionStrategy               Velocity               Patience
126//!                      │                  DoorState               Preferences
127//!                      └── Stop (served stops along the shaft)
128//! ```
129//!
130//! ### Rider lifecycle
131//!
132//! Riders progress through phases managed by the simulation:
133//!
134//! ```text
135//! Waiting → Boarding → Riding → Exiting → Arrived
136//!    ↑         (1 tick)           (1 tick)     │
137//!    │                                         ├── settle_rider() → Resident
138//!    │                                         │                       │
139//!    │                                         └── despawn_rider()     │
140//!    │                                                                 │
141//!    └──────── reroute_rider() ────────────────────────────────────────┘
142//!
143//! Waiting ──(patience exceeded)──→ Abandoned ──→ settle/despawn
144//! ```
145//!
146//! - **`Arrived`** / **`Abandoned`**: terminal states; consumer must explicitly
147//!   settle or despawn the rider.
148//! - **`Resident`**: parked at a stop, invisible to dispatch and loading.
149//!   Query with [`Simulation::residents_at()`](sim::Simulation::residents_at).
150//! - **Population queries**: O(1) via maintained reverse index —
151//!   [`residents_at`](sim::Simulation::residents_at),
152//!   [`waiting_at`](sim::Simulation::waiting_at),
153//!   [`abandoned_at`](sim::Simulation::abandoned_at).
154//!
155//! ### Extension storage
156//!
157//! Games attach custom data to any entity without modifying the library:
158//!
159//! ```rust,no_run
160//! # use elevator_core::prelude::*;
161//! # use elevator_core::__doctest_prelude::*;
162//! # use elevator_core::query::Ext;
163//! # use serde::{Serialize, Deserialize};
164//! # #[derive(Debug, Clone, Serialize, Deserialize)]
165//! # struct VipTag { priority: u32 }
166//! # fn run(world: &mut World, rider_id: EntityId) {
167//! // Attach a VIP flag to a rider.
168//! world.insert_ext(rider_id, VipTag { priority: 1 }, ExtKey::from_type_name());
169//!
170//! // Query it alongside built-in components.
171//! for (id, rider, vip) in world.query::<(EntityId, &Rider, &Ext<VipTag>)>().iter() {
172//!     // ...
173//! }
174//! # }
175//! ```
176//!
177//! Extensions participate in snapshots via `serialize_extensions()` /
178//! `register_ext::<T>(key)` + `load_extensions()`.
179//!
180//! ### Snapshot lifecycle
181//!
182//! 1. Capture: `sim.snapshot()` → [`WorldSnapshot`](snapshot::WorldSnapshot)
183//! 2. Serialize: serde (RON, JSON, bincode, etc.)
184//! 3. Deserialize + restore: `snapshot.restore(factory)` → new `Simulation`
185//! 4. Re-register extensions: `world.register_ext::<T>(key)` per type
186//! 5. Load extension data: `sim.load_extensions()`
187//!
188//! For the common case (save-to-disk, load-from-disk), skip the format choice
189//! and use [`Simulation::snapshot_bytes`](sim::Simulation::snapshot_bytes) /
190//! [`Simulation::restore_bytes`](sim::Simulation::restore_bytes). The byte
191//! blob is postcard-encoded and carries a magic prefix plus the crate version:
192//! restoring bytes from a different `elevator-core` version returns
193//! [`SimError::SnapshotVersion`](error::SimError::SnapshotVersion) instead of
194//! silently producing a garbled sim. Determinism is bit-exact across builds
195//! of the same crate version, which makes snapshots viable as rollback-netcode
196//! checkpoints or deterministic replay fixtures.
197//!
198//! ### Performance
199//!
200//! | Operation | Complexity |
201//! |-----------|-----------|
202//! | Entity iteration | O(n) via `SlotMap` secondary maps |
203//! | Stop-passing detection | O(log n) via `SortedStops` binary search |
204//! | Dispatch manifest build | O(riders) per group |
205//! | Population queries | O(1) via `RiderIndex` reverse index |
206//! | Topology graph queries | O(V+E) BFS, lazy rebuild |
207//!
208//! ## Runtime upgrades
209//!
210//! Elevator kinematic and door-timing parameters can be mutated at runtime
211//! via the `Simulation::set_*` setters — handy for RPG-style upgrade systems
212//! or scripted events that boost speed, capacity, or door behavior mid-game.
213//!
214//! Each setter validates its input, mutates the underlying component, and
215//! emits an [`Event::ElevatorUpgraded`](events::Event::ElevatorUpgraded) so
216//! game code can react (score popups, SFX, UI). Velocity is preserved when
217//! kinematic parameters change — the integrator picks up the new values on
218//! the next tick without jerk. Door-timing changes apply to the next door
219//! cycle and never retroactively retime an in-progress transition.
220//!
221//! See [`examples/runtime_upgrades.rs`][rte] for an end-to-end demonstration
222//! that doubles a car's `max_speed` mid-run and prints the throughput delta.
223//!
224//! [rte]: https://github.com/andymai/elevator-core/blob/main/crates/elevator-core/examples/runtime_upgrades.rs
225//!
226//! ## Door control
227//!
228//! Games that want to drive elevator doors directly — e.g. the player
229//! pressing "open" or "close" on a cab panel in a first-person game, or an
230//! RPG where the player *is* the elevator — use the manual door-control
231//! API on [`Simulation`](sim::Simulation):
232//!
233//! - [`open_door`](sim::Simulation::open_door)
234//! - [`close_door`](sim::Simulation::close_door)
235//! - [`hold_door`](sim::Simulation::hold_door) (cumulative)
236//! - [`cancel_door_hold`](sim::Simulation::cancel_door_hold)
237//!
238//! Each call is either applied immediately (if the car is in a matching
239//! door-FSM state) or queued on the elevator's
240//! [`door_command_queue`](components::Elevator::door_command_queue) and
241//! re-tried every tick until it can be applied. The only hard errors are
242//! [`NotAnElevator`](error::SimError::NotAnElevator) / [`ElevatorDisabled`](error::SimError::ElevatorDisabled) and (for `hold_door`) a
243//! zero-tick argument — the rest return `Ok(())` and let the engine pick
244//! the right moment. A [`DoorCommand`](door::DoorCommand) can be:
245//!
246//! - `Open` — reverses a closing door; no-op if already open or opening;
247//!   queues while the car is moving.
248//! - `Close` — forces an early close from `Loading`. Waits one tick if a
249//!   rider is mid-boarding/exiting (safe-close).
250//! - `HoldOpen { ticks }` — adds to the remaining open dwell; two calls
251//!   of 30 ticks stack to 60. Queues if doors aren't open yet.
252//! - `CancelHold` — clamps any accumulated hold back to the base dwell.
253//!
254//! Every command emits
255//! [`Event::DoorCommandQueued`](events::Event::DoorCommandQueued) when
256//! submitted and
257//! [`Event::DoorCommandApplied`](events::Event::DoorCommandApplied) when
258//! it actually takes effect — useful for driving UI feedback (button
259//! flashes, SFX) without polling the elevator every tick.
260//!
261//! See [`examples/door_commands.rs`][dex] for a runnable demo.
262//!
263//! [dex]: https://github.com/andymai/elevator-core/blob/main/crates/elevator-core/examples/door_commands.rs
264//!
265//! ## Sub-tick position interpolation
266//!
267//! Games that render at a higher framerate than the simulation ticks (e.g.
268//! a 60 Hz sim driving a 144 Hz camera, or a first-person game where the
269//! player is parented to an elevator car) need a smooth position between
270//! ticks. [`Simulation::position_at`](sim::Simulation::position_at) lerps
271//! between the snapshot taken at the start of the current tick and the
272//! post-tick position, using an `alpha` accumulator clamped to `[0.0, 1.0]`:
273//!
274//! ```text
275//! // typical fixed-timestep render loop
276//! accumulator += frame_dt;
277//! while accumulator >= sim.dt() {
278//!     sim.step();
279//!     accumulator -= sim.dt();
280//! }
281//! let alpha = accumulator / sim.dt();
282//! let y = sim.position_at(car, alpha).unwrap();
283//! ```
284//!
285//! The previous-position snapshot is refreshed automatically at the start
286//! of every [`step`](sim::Simulation::step). [`Simulation::velocity`](sim::Simulation::velocity) is
287//! a convenience that returns the raw `f64` along the shaft axis (signed:
288//! +up, -down) for camera tilt, motion blur, or cabin-sway effects.
289//!
290//! See [`examples/fp_player_rider.rs`][fpe] for a runnable demo.
291//!
292//! [fpe]: https://github.com/andymai/elevator-core/blob/main/crates/elevator-core/examples/fp_player_rider.rs
293//!
294//! ## Manual-drive mode
295//!
296//! Games where the player *is* the elevator — driving the car with a
297//! velocity stick, slamming an emergency brake, stopping between floors
298//! — use [`ServiceMode::Manual`](components::ServiceMode::Manual). Manual
299//! elevators are skipped by the automatic dispatch and repositioning
300//! phases; the consumer drives movement via:
301//!
302//! - [`Simulation::set_target_velocity`](sim::Simulation::set_target_velocity) — signed target speed (+up, -down), clamped to the car's `max_speed`.
303//! - [`Simulation::emergency_stop`](sim::Simulation::emergency_stop) — commands an immediate deceleration to zero.
304//!
305//! Physics still apply: velocity ramps toward the target using the car's
306//! `acceleration` / `deceleration` caps, and positions update at the same
307//! rate as normal elevators. Manual elevators can come to rest at any
308//! position — they are not required to align with a configured stop.
309//! Door behaviour is governed by the [manual door-control API](#door-control);
310//! nothing opens or closes automatically. Leaving `Manual` clears any
311//! pending velocity command.
312//!
313//! Each command emits
314//! [`Event::ManualVelocityCommanded`](events::Event::ManualVelocityCommanded)
315//! with the clamped target, or `None` for an emergency stop.
316//!
317//! See [`examples/manual_driver.rs`][mde] for a runnable demo.
318//!
319//! [mde]: https://github.com/andymai/elevator-core/blob/main/crates/elevator-core/examples/manual_driver.rs
320//!
321//! ## ETA queries
322//!
323//! Hall-call dispatch UIs, scheduling overlays, and "press to call" panels
324//! all need the same answer: *how long until this car shows up?* Two methods
325//! on [`Simulation`](sim::Simulation) compute it from the elevator's queued
326//! destinations, current kinematic state, and configured door dwell:
327//!
328//! - [`Simulation::eta`](sim::Simulation::eta) — seconds until a specific
329//!   elevator reaches a specific stop, or an [`EtaError`](error::EtaError) if the
330//!   stop isn't queued, the car is disabled, or the service mode excludes it.
331//! - [`Simulation::best_eta`](sim::Simulation::best_eta) — winner across all
332//!   eligible elevators, optionally filtered by indicator-lamp direction
333//!   ("which up-going car arrives first?").
334//!
335//! Both walk the queue in service order, summing closed-form trapezoidal
336//! travel time per leg plus the configured door cycle at every intermediate
337//! stop. The closed-form solver lives in [`eta::travel_time`] and tracks the
338//! per-tick integrator in [`movement::tick_movement`] to within a tick or
339//! two — close enough for UI countdowns; not a substitute for actually
340//! simulating to compare two dispatch policies.
341//!
342//! For narrative guides, tutorials, and architecture walkthroughs, see the
343//! [mdBook documentation](https://andymai.github.io/elevator-core/).
344
345#![forbid(unsafe_code)]
346#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
347
348/// Single source of truth for the host-binding wire/ABI version.
349///
350/// Every consumer crate (FFI's `EV_ABI_VERSION`, wasm's
351/// `ABI_VERSION`, gdext's `ABI_VERSION`) references this constant
352/// at compile time so they cannot drift apart in-source. The C
353/// header (`elevator_ffi.h`) and the example harnesses still embed
354/// a literal copy; `scripts/check-abi-pins.sh` enforces that those
355/// copies match this value.
356///
357/// Bump when any host-facing wire format changes — `EvEvent`
358/// layout, `EvSnapshot` layout, an enum value re-numbering, etc.
359/// See [Host Binding Parity](https://andymai.github.io/elevator-core/host-binding-parity.html)
360/// for the cross-host contract this constant is part of.
361pub const HOST_PROTOCOL_VERSION: u32 = 5;
362
363#[cfg(doctest)]
364mod doctests;
365
366/// Entity-component data types for the simulation.
367pub mod components;
368/// Entity identity and allocation.
369pub mod entity;
370/// Simulation error types.
371pub mod error;
372/// Typed identifiers for groups and other sim concepts.
373pub mod ids;
374/// ECS-style query builder for iterating entities by component composition.
375pub mod query;
376/// Tick-loop system phases (dispatch, reposition, movement, doors, loading, metrics).
377pub mod systems;
378/// Central entity/component storage.
379pub mod world;
380
381/// Rolling per-stop arrival log, the signal source for rate-driven
382/// dispatch and repositioning.
383pub mod arrival_log;
384/// Fluent builder for constructing a Simulation programmatically.
385pub mod builder;
386/// Building and elevator configuration (RON deserialization).
387pub mod config;
388/// Pluggable dispatch strategies (SCAN, LOOK, nearest-car, ETD).
389pub mod dispatch;
390/// Door finite-state machine.
391pub mod door;
392/// Simplified energy modeling for elevators.
393#[cfg(feature = "energy")]
394pub mod energy;
395/// ETA estimation for queued elevators (closed-form trapezoidal travel time).
396pub mod eta;
397/// Simulation event bus and event types.
398pub mod events;
399/// Floating-point helpers gated by the `deterministic-fp` feature.
400pub(crate) mod fp;
401/// Lifecycle hooks for injecting logic before/after simulation phases.
402pub mod hooks;
403/// Aggregate simulation metrics.
404pub mod metrics;
405/// Trapezoidal velocity-profile movement math.
406///
407/// Exposes [`braking_distance`](movement::braking_distance) for consumers
408/// writing opportunistic dispatch strategies that need the kinematic answer
409/// without constructing a `Simulation`.
410pub mod movement;
411/// Phase-partitioned reverse index for rider population queries.
412mod rider_index;
413/// Scenario replay from recorded event streams.
414pub mod scenario;
415/// Top-level simulation runner.
416pub mod sim;
417/// World snapshot for save/load.
418pub mod snapshot;
419/// Stop configuration helpers.
420pub mod stop;
421/// Tag-based per-entity metrics.
422pub mod tagged_metrics;
423/// Tick-to-wall-clock time conversion.
424pub mod time;
425/// Topology graph for cross-line connectivity queries.
426pub mod topology;
427/// Traffic generation (arrival patterns).
428#[cfg(feature = "traffic")]
429pub mod traffic;
430/// Traffic-mode detector (up-peak / idle / inter-floor).
431pub mod traffic_detector;
432
433/// Register multiple extension types for snapshot deserialization in one call.
434///
435/// Eliminates the manual `register_ext` ceremony after snapshot restore.
436///
437/// # Examples
438///
439/// Name-less form (uses `type_name` automatically):
440///
441/// ```
442/// use elevator_core::prelude::*;
443/// use elevator_core::register_extensions;
444/// use serde::{Deserialize, Serialize};
445///
446/// #[derive(Clone, Debug, Serialize, Deserialize)]
447/// struct VipTag { level: u32 }
448///
449/// #[derive(Clone, Debug, Serialize, Deserialize)]
450/// struct Priority { rank: u8 }
451///
452/// let mut sim = SimulationBuilder::demo().build().unwrap();
453/// register_extensions!(sim.world_mut(), VipTag, Priority);
454/// ```
455///
456/// Named form (explicit storage name per type):
457///
458/// ```
459/// use elevator_core::prelude::*;
460/// use elevator_core::register_extensions;
461/// use serde::{Deserialize, Serialize};
462///
463/// #[derive(Clone, Debug, Serialize, Deserialize)]
464/// struct VipTag { level: u32 }
465///
466/// let mut sim = SimulationBuilder::demo().build().unwrap();
467/// register_extensions!(sim.world_mut(), VipTag => "vip_tag");
468/// ```
469#[macro_export]
470macro_rules! register_extensions {
471    ($world:expr, $($ty:ty),+ $(,)?) => {
472        $( $world.register_ext::<$ty>($crate::world::ExtKey::<$ty>::from_type_name()); )+
473    };
474    ($world:expr, $($ty:ty => $name:expr),+ $(,)?) => {
475        $( $world.register_ext::<$ty>($crate::world::ExtKey::<$ty>::new($name)); )+
476    };
477}
478
479/// Common imports for consumers of this library.
480///
481/// `use elevator_core::prelude::*;` pulls in the 22 names that cover the
482/// vast majority of usage: building a sim, stepping it, querying world
483/// state, writing custom dispatch, and reading aggregate metrics. Anything
484/// beyond that — fine-grained component types, per-strategy structs,
485/// extension keys, traffic generation — is imported explicitly from its
486/// sub-module.
487///
488/// # Contents
489///
490/// - **Sim & builder:** [`Simulation`](crate::sim::Simulation),
491///   [`SimulationBuilder`](crate::builder::SimulationBuilder),
492///   [`RiderBuilder`](crate::sim::RiderBuilder)
493/// - **Config:** [`SimConfig`](crate::config::SimConfig),
494///   [`ElevatorConfig`](crate::config::ElevatorConfig),
495///   [`StopConfig`](crate::stop::StopConfig)
496/// - **Identity:** [`StopId`](crate::stop::StopId),
497///   [`ElevatorId`](crate::entity::ElevatorId),
498///   [`RiderId`](crate::entity::RiderId),
499///   [`EntityId`](crate::entity::EntityId),
500///   [`GroupId`](crate::ids::GroupId)
501/// - **Errors & events:** [`SimError`](crate::error::SimError),
502///   [`RejectionReason`](crate::error::RejectionReason),
503///   [`Event`](crate::events::Event)
504/// - **Phase & direction enums:** [`ElevatorPhase`](crate::components::ElevatorPhase),
505///   [`RiderPhase`](crate::components::RiderPhase),
506///   [`Direction`](crate::components::Direction)
507/// - **Dispatch:** [`DispatchStrategy`](crate::dispatch::DispatchStrategy),
508///   [`BuiltinStrategy`](crate::dispatch::BuiltinStrategy),
509///   [`BuiltinReposition`](crate::dispatch::BuiltinReposition)
510/// - **World & metrics:** [`World`](crate::world::World),
511///   [`Metrics`](crate::metrics::Metrics)
512///
513/// # Not included (import explicitly)
514///
515/// - Fine-grained component types (`Position`, `Velocity`, `Speed`, `Accel`,
516///   `Weight`, `Route`, `Patience`, `ServiceMode`, etc.) → [`crate::components`]
517/// - Per-strategy structs (`ScanDispatch`, `LookDispatch`,
518///   `NearestCarDispatch`, `EtdDispatch`, `RsrDispatch`, `DestinationDispatch`),
519///   `RankContext`, `DispatchManifest`, reposition strategies → [`crate::dispatch`]
520/// - Extension storage (`ExtKey`) → [`crate::world`]
521/// - Traffic detection, arrival logs, time adapters, event bus,
522///   group/line config, and `StopRef` → their respective modules
523/// - Traffic generation types from [`crate::traffic`] (feature-gated)
524/// - Snapshot types from [`crate::snapshot`]
525pub mod prelude {
526    pub use crate::builder::SimulationBuilder;
527    pub use crate::components::{Direction, ElevatorPhase, RiderPhase};
528    pub use crate::config::{ElevatorConfig, SimConfig};
529    pub use crate::dispatch::{BuiltinReposition, BuiltinStrategy, DispatchStrategy};
530    pub use crate::entity::{ElevatorId, EntityId, RiderId};
531    pub use crate::error::{RejectionReason, SimError};
532    pub use crate::events::Event;
533    pub use crate::ids::GroupId;
534    pub use crate::metrics::Metrics;
535    pub use crate::sim::{RiderBuilder, Simulation};
536    pub use crate::stop::{StopConfig, StopId};
537    pub use crate::world::World;
538}
539
540/// Internal-only re-exports used by hidden doc-test setup lines
541/// (`# use elevator_core::__doctest_prelude::*;`) so each chapter's
542/// fenced blocks compile without per-block import surgery while the
543/// public [`prelude`] stays small. Hidden from `cargo doc` and
544/// considered an implementation detail — not part of the public API.
545///
546/// Names already in [`prelude`] are deliberately omitted to avoid
547/// glob-import ambiguity errors when both modules are wildcarded.
548#[doc(hidden)]
549pub mod __doctest_prelude {
550    pub use crate::arrival_log::{ArrivalLog, ArrivalLogRetention, CurrentTick, DestinationLog};
551    pub use crate::components::{
552        Accel, AccessControl, DestinationQueue, Elevator, Line, Orientation, Patience, Position,
553        Preferences, Rider, RiderPhaseKind, Route, ServiceMode, SpatialPosition, Speed, Stop,
554        UnitError, Velocity, Weight,
555    };
556    pub use crate::config::{GroupConfig, LineConfig};
557    pub use crate::dispatch::reposition::{
558        AdaptiveParking, DemandWeighted, NearestIdle, PredictiveParking, ReturnToLobby,
559        SpreadEvenly,
560    };
561    pub use crate::dispatch::{
562        AssignedCar, DestinationDispatch, DispatchDecision, DispatchManifest, ElevatorGroup,
563        EtdDispatch, LookDispatch, NearestCarDispatch, RankContext, RepositionStrategy,
564        RsrDispatch, ScanDispatch,
565    };
566    pub use crate::error::{EtaError, RejectionContext};
567    pub use crate::events::{EventBus, EventCategory};
568    pub use crate::stop::StopRef;
569    pub use crate::time::TimeAdapter;
570    pub use crate::traffic_detector::{TrafficDetector, TrafficMode};
571    pub use crate::world::ExtKey;
572}
573
574#[cfg(test)]
575mod tests;